码迷,mamicode.com
首页 > 数据库 > 详细

Sql Server数据库之存储过程

时间:2019-01-13 16:12:25      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:tst   entity   stun   sid   exe   exec   数据库   and   输入   

 

一.存储过程

  1.存储过程定义:一组为了完成特定功能的Sql语句集合,经编译后存储在服务器端的数据库中,利用存储过程可以加速Sql语句的执行

  2.无参数的存储过程   

--创建无参数的存储过程
create procedure GetStuCou
as
begin
select * from student s
left join class c on s.classId = c.classId
end
execute GetStuCou

  3.有返回值的存储过程

1 --创建名为GetStuCou_Re的有返回值的存储过程
2 create procedure GetStuCou_Re
3 as
4 begin 
5     insert into student values(8,栾云平,3);
6     return scope_identity();
7 end
8 execute GetStuCou_Re

  4.有输入参数的存储过程

 1 --创建名为GetStuCou_In的有输入参数的存储过程
 2 create procedure GetStuCou_In
 3 @StuNo nvarchar(64) = 001 --设置默认值
 4 as
 5 begin
 6     select * from student where studentId = @StuNo
 7 end
 8 --执行名为GetStuCou_In的有输入参数的存储过程(不传参数,即使用默认值)
 9 execute GetStuCou_In
10 --执行名为GetStuCou_In的有输入参数的存储过程(传入参数)
11 execute GetStuCou_In 005

技术分享图片

  5.有输入,输出参数的存储过程

 1 --创建名为GetStuCou_Out的有输入参数和输出参数的存储过程
 2 create procedure GetStuCou_Out
 3 @StuNo nvarchar(64),
 4 @Name nvarchar(64) output
 5 as
 6 begin
 7     if(@StuNo is not null and @StuNo <> ‘‘)
 8     begin 
 9         select @Name = studentName
10         from student
11         where studentId = @StuNo
12     end
13     else
14     begin
15         set @Name=0
16     end
17     print @Name
18 end
19 --执行名为GetStuCou_Out的有输入参数和输出参数的存储过程
20 execute GetStuCou_Out 005,null

技术分享图片

  6.有输入,输出参数和结果集的存储过程

 

     

Sql Server数据库之存储过程

标签:tst   entity   stun   sid   exe   exec   数据库   and   输入   

原文地址:https://www.cnblogs.com/alan-1996/p/10262668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!