Sql Server2008温故而知新系列11:存储过程

存储过程个人理解就是一段指令的集合,这段指令集里面可以有变量、增删改查语句、流程控制、循环语句等

在SQL SERVER中创建过程的命令create proc[edure] proc_name [参数名1 type],[参数名2 type] as begin ………………end

附个简单的例子:

 1 use myDB
 2 go
 3 create proc p_test
 4 @name varchar(20),
 5 @age smallint 
 6 as 
 7 begin
 8 if exists(select name from tstb where name=@name)
 9     begin
10         update tstb set age = @age where name = @name
11     end
12 else
13     begin
14         insert into tstb(name,age) values (@name,@age)
15     end
16 select * from tstb where name = @name
17 end
18 
19 go
20 p_test 'John',30

 

上述很简单的过程,修改表中指定人员的年龄;如果指定人员不存在,则表中插入该人员及年龄;

第19,20行;执行过程命令: [execute/exec] procedure_name [参数1],[参数2],[参数N]

exec/execute 也可以直接省略 直接写过程名后加参数;

p_test 'john',30 指定tstb表中的john的年龄为30,如果tstb表中没有john,那么新增John,年龄30;

然后查询tstb表中John的信息。

posted @ 2019-12-01 11:16  無尘  阅读(193)  评论(0编辑  收藏  举报