while 循环,存储过程

1、while 循环

declare @ss int
set @ss=2
while @ss<10
begin
set @ss=@ss+1
print 'HELLO'+convert(char(10),@ss)
if @ss=7
break
end


declare @sss int
set @sss=2
while @sss<10
begin
set @sss=@sss+1
if @sss=7
continue
print 'HELLO'+convert(char(10),@sss)
end

 

--查询总分最高的学生的语文教师的所有信息
select*from teacher where tcode=
(select yujiao from student where xcode=
(select top 1 fcode from score group by fcode order by SUM(shufen+yufen+yingfen)desc))

 

2、存储过程

①    没有参数,没有返回值

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

 

② 有参数 ,没有返回值

--查看所输入编号的学生是否能够结业,两门以上及格即可结业
--三门都及格,【优秀】
--两门及格,【结业】
--一门及格,【不结业】
--三门都不及格,【请重修】
alter proc xinproc
@shu int
as
declare @shufen decimal(18,2),@yufen decimal(18,2),@yingfen decimal(18,2)
select @shufen=shufen,@yufen=yufen,@yingfen=yingfen from score where fcode=@shu
declare @sum int
set @sum=0
if @shufen>=60
set @sum+=1
if @yufen>=60
set @sum+=1
if @yingfen>=60
set @sum+=1
if @sum=3
print '优秀'
if @sum=2
print '及格'
if @sum=1
print '不及格'
if @sum=0
print '重修'
go
exec xinproc 2

 

③  有一个参数,有返回值

--输入一个学生的学号,想要经过存储过程之后得到在这个学生的总分

create proc firstproc1
@shu int
as
declare @sum decimal(18,2)
select @sum=SUM(shufen+yufen+yingfen)from score where fcode=@shu
return @sum
go
declare @fan decimal(18,2)
exec @fan=firstproc1 2 --定义一个变量接收
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

 

实例:

--存储过程练习:输入一个数,求1~n的和

alter proc sec
@n int
as
declare @sum int,@i int
set @sum=0
set @i=1
while @i<=@n
begin
set @sum=@sum+@i
set @i=@i+1
end
return @sum
go
declare @fan int
exec @fan=sec 4
print @fan

 

--存储过程练习:输入一个数求这个1!+2!+...+n!的阶乘

create proc m
@n int
as
declare @sum int,@jie int,@i int
set @sum=0
set @jie=1
set @i=1
while @i<=@n
begin
set @jie=@jie*@i
set @sum=@sum+@jie
set @i=@i+1
end
return @sum
go
declare @fan int
exec @fan=m 3
print @fan

 

转载于:https://www.cnblogs.com/liujianshe1990-/p/4992796.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值