SQL Server中的流控制语句

  • 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中的流控制语句介绍的这里。

posted @ 2018-08-19 11:59  paulhe  阅读(802)  评论(0编辑  收藏  举报
friendster counter