当前位置:Gxlcms > 数据库问题 > Sql Server2008温故而知新系列11:存储过程

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

时间:2021-07-01 10:21:17 帮助过:7人阅读

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的信息。

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

标签:温故而知新   varchar   sts   个人   sel   test   proc   exe   div   

人气教程排行