1. 程式人生 > >SQL 遊標的寫法

SQL 遊標的寫法

定義 rst div all art status datetime cat lar

DECLARE  @Y1002 varchar(200),@A0100 varchar(200),@C0103 datetime                   ----定義變量
DECLARE  Police_Department CURSOR FOR                                       --定義遊標
SELECT Y1002,A0100,C0103 FROM A001A000  WHERE LEFT(Y1002,1)=‘4‘ AND LaborState=1             ---查詢的數據
OPEN Police_Department                                                     --打開遊標
FETCH next from Police_Department into @Y1002,@A0100,@C0103                      --將遊標向下移1行,獲取的數據放入之前定義的變量@@Y1002,@A0100,@C0103中,這個支段的數量要與查出的支段數量一致
WHILE @@fetch_status=0                                                     --判斷是否成功獲取數據
	BEGIN 
	                                                     -----這裏寫代碼邏輯塊    
		FETCH  NEXT FROM  Police_Department into @Y1002,@A0100,@C0103           --將遊標向下移1行
	END  
CLOSE Police_Department                                           --關閉遊標
DEALLOCATE Police_Department                                        --釋放遊標
---遊標可用於循環

  

SQL 遊標的寫法