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

  • 相关阅读:
    条件语句实例
    数据类型
    C#与.NET概述
    c#循环
    语句
    数组

    英文文献中的数学符号
    如何计算协方差、 协方差矩阵 、 相关系数 、 马氏距离
    opengl 笔记
  • 原文地址:https://www.cnblogs.com/paulhe/p/9499879.html
Copyright © 2020-2023  润新知