1 游標的作用: 2 1.定位到結果集中的某一行。 3 2.對當前位置的數據進行讀寫。 4 3.可以對結果集中的數據單獨操作,而不是整行執行相同的操作。 5 4.是面向集合的數據庫管理系統和面向行的程序設計之間的橋梁。 6
7 游標的種類: 8 1.靜態游標:操作結果在重新打開才有效,消耗少 9 2.動態游標:操作結果在下一次操作可用,消耗多 10 3.只進游標:從頭到尾提取數據,操作結果提取時可見,提取后不可見 11 4.鍵集Y驅動游標:操作結果被標識的數據可見,未被標識的數據不可見,消耗適中 14
15 定義一個游標: 16 declare cursor_name cursor [cursor_type] (cursor_type有static,dynamic,forward_only,keyset) 17 for 查詢語句結果集 18
19 打開一個游標:
20 open cursor_name 21
22 使用游標提取數據方式: 23 fetch 某參數 from cursor_name into 變量列表 24 某參數說明: 25 1.first:定位到結果集第一行 26 2.last:定位到結果集的最后一行 27 3.prior:定位到上一行 28 4.next:定位到下一行 29 5.absoute n:定位到從第一行開始第n行 30 6.relative n:定位到從當前位置開始第n行 31
32 游標相關狀態全局變量使用: 33 建立一個循環讀取可能: 34 declare 參數1 參數類型,參數2 參數類型... 35 fetch first from cursor_name into 參數1 參數類型,參數2 參數類型... 36 while @@fetch_status = 0
37 begin 38 print '參數信息'
39 fetch next from cursor_name into 參數1 參數類型,參數2 參數類型... 40 end 41
42 更新、刪除游標數據: 43 update table_name set colume_name = value[,...] where current of cursor_name 44 delete table_name where current of cursor_name 45
46 關閉、刪除游標: 47 close cursor_name 48 deallocate cursor_name