SQL Server中的流控制語句
阿新 • • 發佈:2018-08-19
weight int return pri use 可選 查詢 一個 pan
- 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中的流控制語句