SQL Server中的流控制语句

  • begin···end

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

declare @num int ;
set @num=0;

while  @num<10

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

end

 

  • if···else

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

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 wordcode

 

  • goto label(自定义标记)

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

declare @num int;
set @num=0;

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

while  @num<10
begin
  goto echo
end

 

  • return

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

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'表示要等到哪一个时刻执行。class

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

waitfor delay '00:00:10'
print 'hello word' 

--

print 'hello word' waitfor delay '00:00:10'

 

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

waitfor time '12:00:00'
print 'hello word' 

--

print 'hello word' waitfor time '12:00:00'

 

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

相关文章
相关标签/搜索