首页 > 数据库技术 > 详细

SQL Server中的流控制语句

时间:2018-08-19 12:18:43      阅读:189      评论:0      收藏:0      [点我收藏+]
  • begin···end

该语句定义sql代码块,通常在if和while语句中使用

declare @num int ;
set @num=0;

while  @num<10

begin
  set @num=@num+1;
  print hello word

end

 

  • if···else

条件判断语句,其中else是可选的

if  (select sex from UserBasic where name=张三)=1
    print 张三的性别是:男
else
    print 张三的性别是:女

 

  •  while、break、continue
declare @num int ;
set @num=0;

while  @num<10

begin
  set @num=@num+1;
  print hello word
    if @num=2
        continue
    if @num=5
        break
end

说明:本例输出5行 hello word

 

  • goto label(自定义标记)

该语句用来无条件地将语句的执行顺序转到用户定义的lable处

declare @num int;
set @num=0;

echo:
    print hello word
set @num=@num+1;

while  @num<10
begin
  goto echo
end

 

  • return

该语句用来无条件退出一个查询或一个过程

declare @num int ;
set @num=0;

while  @num<10

begin
  set @num=@num+1;
  print hello word
    if @num=5
        return
end

 

  • waitfor delay/time

该语句用来定义某天的一个时刻,执行一个语句块。waitfor delay ‘time‘表示要等待多长时间,waitfor time ‘time‘表示要等到哪个时刻执行。

示例:10秒之后输出‘hello word’

waitfor delay 00:00:10
print hello word 

--

print hello word waitfor delay 00:00:10

 

示例:12:00钟输出‘hello word’

waitfor time 12:00:00
print hello word 

--

print hello word waitfor time 12:00:00

 

SQL Server中的流控制语句介绍的这里。

SQL Server中的流控制语句

原文:https://www.cnblogs.com/paulhe/p/9499879.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!