存储过程

--存储过程
create proc firstproc--创建一个存储过程
as --存储过程关键字
select * from student--存储过程的语句
go
--执行存储过程的语句(两个都可以)
exec firstproc
execute firstproc

--存储过程可以有返回值
--定义一个变量去接收
declare @fanhui int
exec @fanhui = firstproc--需要执行之后才会有返回值,用变量接收
select @fanhui as 返回值 --查看返回值

--修改存储过程的语句
alter proc firstproc
as
select * from score
go

--利用存储过程查找三个表内的信息
create proc secondproc
as
select * from student
select * from teacher
select * from score
go

--执行
exec secondproc

--利用存储过程查找语文教师张晓华所教课程的学生的分数,
--过80的算优秀,优秀人数超过3个人即为【教师评测达标】
--若不到三个人,【不达标】
create proc thirdproc
as
declare @code int
select @code=code from teacher where name ='张晓华'
declare @count int
select @count= COUNT(*) from score where code 
in(select code from student where yujiao = @code)
and yufen>80
if @count >3
print '教师评测达标'
else
print '教师评测不达标'
go

exec thirdproc


--带一个参数的存储过程
create proc fourthproc
@one char(10) --as前面写上参数的名称,类型
as
print @one--存储过程中就可以使用上面的变量
go
--执行
exec fourthproc '你好啊' --执行时需要将此参数传给存储过程

--带两个参数的存储过程
create proc fifthproc
@two varchar(50), --两个参数或者多个参数时中间用,隔开
@three varchar(50)
as
print @two + @three
go
--执行
exec fifthproc '你好啊!','你在干嘛?'--两个参数用逗号隔开


--查询学号为我们输入的号码的学生的数学成绩
create proc sixproc
@one int
as
select shufen from score where code=@one
go
--执行
exec sixproc 8

--练习:存储过程
--查看所输入编号的学生是否能够结业,两门以上及格即可结业
--三门都及格,【优秀】
--两门及格,【结业】
--一门及格,【不结业】
--三门都不及格,【请重修】
create proc eighthproc
@code int
as
declare @yu decimal(18,2),@shu decimal(18,2),@ying decimal(18,2)
select @yu=yufen from score where code=@code --分别查询语数英的分数
select @shu=shufen from score where code=@code
select @ying=yingfen from score where code=@code
declare @count int--定义标记变量
set @count=0 --标记变量在下面需要先使用再赋值,所以先给他为0
if @yu>=60 --判断语数英是否及格
set @count=@count+1--及格的时候count+1
if @shu>=60
set @count=@count+1
if @ying>=60
set @count=@count+1 

if @count=3--判断count的值:判断几门课及格
print '优秀'
else if @count=2
print '结业'
else if @count=1
print '不结业'
else 
print '请重修'
go
exec eighthproc 6

--不带参数带返回值的存储过程
create proc elevenproc
as
return 5
go
--执行
--需要一个变量来接收这个返回值
declare @fan int
exec @fan= elevenproc
print @fan


--带参数,带返回值的存储过程
create proc twelveproc
@one int,
@two int
as
declare @sum int
set @sum = @one +@two
return @sum
go
--执行
declare @fanhuizonghe int
exec @fanhuizonghe = twelveproc 2,4
print @fanhuizonghe

--输入一个学生的学号,想要经过存储过程之后得到在这个学生的总分
create proc oneproc
@code int
as
declare @yu int
select @yu=yufen from score where code=@code
declare @shu int
select @shu=shufen from score where code=@code
declare @ying int
select @ying=yingfen from score where code=@code
declare @sum int
select @sum=@yu+@shu+@ying
return @sum
go
declare @fan int
exec @fan = oneproc 5
print @fan

--在创建存储过程时,我们可以设置它有一个默认值。
create proc twoproc
@sum int =10 --设置默认值
as
set @sum =@sum +10
return @sum
go
--执行
declare @sumfan int
exec @sumfan= twoproc --可以不写默认值的参数,或者写上default 缺省,默认
print @sumfan


--存储过程练习:输入一个数,求1~n的和
create proc threeproc
@shu int
as
declare @i int,@sum int
set @i=0
set @sum=0
while @i<=@shu
begin
set @sum=@sum+@i
set @i=@i+1
end
return @sum
go
declare @he int
exec @he=threeproc 10
print @he


--存储过程练习:输入一个数求这个1!+2!+...+n!的阶乘
create proc cproc
@n int
as
declare @i int,@jie int,@sum int
set @i=1
set @jie=1
set @sum=0
while @i<=@n
begin
set @jie*=@i
set @sum+=@jie
set @i=@i+1
end
return @sum
go

declare @jiefan int
exec @jiefan=cproc 3
print @jiefan

原文地址:https://www.cnblogs.com/dulovexin/p/4995003.html