SQL SERVER 游標迴圈讀取表資料

2022-01-16 06:52:57 字數 363 閱讀 2546

【cursor】游標:用於迴圈錶行資料,類似指標

格式如下:

declare tempindex cursor for (select * from table) --定義游標

open tempindex --開啟游標

fetch next from tempindex into @x --抓取下一行資料給變數

while @@fetch_status=0 --0表示抓取成功,1表示抓取失敗,2表示不存在抓取行

begin

--sql 語句

endclose tempindex --關閉游標

deallocate tempindex --釋放游標

sqlserver通過游標迴圈查詢

declare id int declare tempcursor cursor for select id from hrmresource where status in 0,1,2,3 order by id 建立游標tempcursor,並定義游標所指向的集合 open tempcursor...

sqlserver游標使用和迴圈

游標說簡單點都是設定乙個資料表的行指標,然後使用迴圈等運算元據 游標主要是用來完成複雜的業務邏輯 比如sqlserver中樹型查詢,比如如下業務點 資料表編號 名稱 父編號 1 中國 0 2 上海市 1 3 虹口區 2 4 楊浦區 2 顯示結果 中國 上海市 虹口區 中國 上海市 楊浦區 類似這樣的...

SQL SERVER迴圈遍歷(普通迴圈和游標迴圈)

自 1 首先需要乙個測試表資料student 2 普通迴圈 1 迴圈5次來修改學生表資訊 迴圈遍歷修改記錄 declare i int set i 0 while i 5 begin update student set demo i 5 where uid i set i i 1 end 檢視結果...