SqlServer 使用存储过程实现插入或更新语句

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

SqlServer 使用存储过程实现插入或更新语句

存储过程的功能非常强大,在某种程度上甚至可以替代业务逻辑层,

接下来就一个小例子来说明,用存储过程插入或更新语句。

1、数据库表结构

2、创建存储过程

1Create proc[dbo].[sp_Insert_Student]

2@No char(10),

3@Name varchar(20),

4@Sex char(2),

5@Age int,

6@rtn int output

7as

8declare

9@tmpName varchar(20),

10@tmpSex char(2),

11@tmpAge int

12

13if exists(select*from Student where No=@No)

14begin

15select@tmpName=Name,@tmpSex=Sex,@tmpAge=Age from Student where No=@No

16if ((@tmpName=@Name) and (@tmpSex=@Sex) and

(@tmpAge=@Age))

17begin

18set@rtn=0

19end

20else

21begin

22update Student set Name=@Name,Sex=@Sex,Age=@Age

where No=@No

23set@rtn=2

24end

25end

26else

27begin

28insert into Student values(@No,@Name,@Sex,@Age) 29set@rtn=1

30end

3、调用存储过程

1declare@rtn int

2exec sp_Insert_Student '1101','张三','男',23,@rtn output

3

4if@rtn=0

5print'已经存在相同的。'

6else if@rtn=1

7print'插入成功。'

8else

9print'更新成功'

相关文档
最新文档