該語句定義sql代碼塊,一般在if和while語句中使用sql
declare @num int ; set @num=0; while @num<10 begin set @num=@num+1; print 'hello word' end
條件判斷語句,其中else是可選的spa
if (select sex from UserBasic where name='張三')=1 print '張三的性別是:男' else print '張三的性別是:女'
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
該語句用來無條件地將語句的執行順序轉到用戶定義的lable處blog
declare @num int; set @num=0; echo: print 'hello word' set @num=@num+1; while @num<10 begin goto echo end
該語句用來無條件退出一個查詢或一個過程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 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中的流控制語句介紹的這裏。查詢