SQL 游標的使用

2021-06-29 04:14:41 字數 3344 閱讀 7254

我們都知道在關聯式資料庫中,都是面向集合進行查詢的,而游標卻是化整為零,是按行查詢的,舉個例子比如說之前那個壕買了99臺蘋果6,他可以一次性就買了99臺,這正是我們平常使用sql的方式,他也可以分成若干次買,這就是游標的方式,一次只查詢一行。

-- 游標分為游標型別和游標變數,對於游標變數來說,游標變數支援兩種方式賦值,定義時賦值和先定義後賦值,

--定義游標變數像定義其他區域性變數一樣,在游標前加」@」,注意,如果定義全域性的游標,只支援定義時直接賦值,

--並且不能在游標名稱前面加「@」,兩種定義方式如下:

--declare cursor_name cursor [ local | global ] 

--     [ forward_only | scroll ] 

--     [ static | keyset | dynamic | fast_forward ] 

--     [ read_only | scroll_locks | optimistic ] 

--     [ type_warning ] 

--     for select_statement 

--     [ for update [ of column_name [ ,...n ] ] ]

其中各引數的含義:

static  keyset  dynamic  和 fast_forward 四選一

這四個關鍵字是游標所在資料集所反應的表內資料和游標讀取出的資料的關係

static意味著,當游標被建立時,將會建立for後面的select語句所包含資料集的副本存入tempdb資料庫中,任何對於底層表內資料的更改不會影響到游標的內容.

dynamic是和static完全相反的選項,當底層資料庫更改時,游標的內容也隨之得到反映,在下一次fetch中,資料內容會隨之改變

keyset可以理解為介於static和dynamic的折中方案。將游標所在結果集的唯一能確定每一行的主鍵存入tempdb,當結果集中任何行改變或者刪除時,@@fetch_status會為-2,keyset無法探測新加入的資料

fast_forward可以理解成forward_only的優化版本.forward_only執行的是靜態計畫,而fast_forward是根據情況進行選擇採用動態計畫還是靜態計畫,大多數情況下fast_forward要比forward_only效能略好.

read_only  scroll_locks  optimistic 三選一 

read_only意味著宣告的游標只能讀取資料,游標不能做任何更新操作

scroll_locks是另一種極端,將讀入游標的所有資料進行鎖定,防止其他程式進行更改,以確保更新的絕對成功

optimistic是相對比較好的乙個選擇,optimistic不鎖定任何資料,當需要在游標中更新資料時,如果底層表資料更新,則游標內資料更新不成功,如果,底層表資料未更新,則游標內錶資料可以更新

--定義後直接賦值全域性游標,在批處理結束之後依然存在

--預設和forward_only只支援fetch next 選項;scroll 支援向任何方向移動

declare goods_cursorg cursor global scroll for 

select goodsid from t_goods

--先定義後賦值,區域性游標,在批處理結束後被隱式釋放,無法再讓其他批處理中呼叫

--declare @goods_cursort cursor

--set @goods_cursort= cursor for

--select * from t_goods

go--open goods_cursort

open goods_cursorg

--fetch last from goods_cursort

--fetch last from goods_cursorg

--取下一行

declare @nextrow varchar(200)

fetch next from goods_cursorg into @nextrow

print @nextrow

--取最後一行

declare @lastrow varchar(200)

fetch last from goods_cursorg into @lastrow

print @lastrow

--取第一行

declare @firstrow varchar(200)

fetch first from goods_cursorg into @firstrow

print @firstrow

--取上一行

declare @prerow varchar(200)

fetch prior from goods_cursorg into @prerow

print @prerow

--取第三行

declare @thirdrow varchar(200)

fetch absolute 3 from goods_cursorg into @thirdrow

print @thirdrow

--取相對當前來說上一行

declare @relativerow varchar(200)

fetch relative -1 from goods_cursorg into @relativerow

print @relativerow

--關閉游標

--close goods_cursorg

--釋放游標

--deallocate goods_cursorg

游標經常會和全域性變數@@fetch_status與while迴圈來共同使用,以達到遍歷游標所在資料集的目的

declare good_cursorg cursor global scroll for 

select goodsid,goodstype from t_goods

open good_cursorg

declare @id nvarchar(200)

declare @type nvarchar(200)

while @@fetch_status=0

begin

print @id

print @type

fetch next from good_cursorg into @id,@type

endclose good_cursorg

deallocate good_cursorg

ps:fetch的一些引數:

SQL游標的使用

sql游標的使用 2008 09 29 13 57 一 游標包括兩個部分 1 游標結果集 由定義該游標的select語句返回的行的集合 2 游標位置 指向這個集合中某行的指標 二 游標處理過程 使用declare 語句宣告 使用open語句開啟 使用fecth語句從游標中提取資料 判斷是否為空,為空...

SQL游標的使用

table1結構如下 id int name varchar 50 declare id int declare name varchar 50 declare cursor1 cursor for 定義游標cursor1 select from table1 使用游標的物件 跟據需要填入selec...

SQL迴圈游標的使用

今天搞的,幫同事查詢資料,先寫個觸發器,擷取http www.chinaroyalgroup.cn aspnet client system web 2 0 50727 dispbbs.asp?boardid 1 id 836裡的網域名稱 if exists select name from sys...