SQL SERVER中的流程控制语句
时间:2022-03-14 17:38
begin --<sql语句或程序块> end
begin
--<sql语句或程序块>
select * from StudentInfo
update StudentInfo set money =50
end
if<条件表达式> --<sql语句或程序块> else<条件表达式> -- <sql语句或程序块>
--declare 是声明的意思
declare @money int
select @money=money from StudentInfo where stuid ='01'
if @money >20
print '钱太多了'
else
print '钱太少了'
if [not]exists (select 查询语句)
<命令行或语句块>
else <条件表达式>
<命令行或语句块>
--检查学号为01的学生是否存在 if exists (select * from StudentInfo where stuid='01') print '这个学生存在' else print '这个学生不存在'
case<算术表达式> when<算术表达式>then<运算符> when<算术表达式>then<运算符> [else<算术表达式>] end
case when<算术表达式>then<运算符> when<算术表达式>then<运算符> [else<算术表达式>] end
--选择某一条件 select money= case --对该条件进行选择分支判断 when(money<20) then '太少了' when(money>20) then '太多了' else '还行吧' end from StudentInfo
while <条件表达式> begin <sql语句或者程序块> break continue <sql语句或者程序块> end
--计算1+2+3……100的和 declare @i int,@small int select @i=1,@small=0 while @i<=100 --判断的条件 begin set @small =@small +@i set @i=@i+1 continue end print '1+2+3……100的和是' print @small
--计算1+2+3……100的和 declare @i int,@small int select @i=1,@small=0 while @i<=100 --判断的条件 begin set @small =@small +@i set @i=@i+1 goto wode continue end print '1+2+3……100的和是' print @small wode: print '我跳出来了'
小结:
这些语句时我们在学习SQL SERVER中会经常遇到的,其实也不难,与我们平常学习的编程语言中的控制结构相同,只不过是格式可能的略作修改,只要尝试下,找几个例子练一下即可。
SQL SERVER中的流程控制语句,布布扣,bubuko.com