SQL Server 中游標的使用

2022-03-16 02:48:28 字數 4107 閱讀 5129

sql server 中游標的使用

1.游標是行讀取,占用資源比sql多

2.游標的使用情景:

->現存的系統中使用的是游標,查詢必須通過游標來實現

->用盡了while、子查詢臨時表、表變數、自定義函式以及其他方式仍然無法實現的時候,使用游標

3.t-sql 中游標的生命週期由5部分組成

->定義游標:游標的定義遵循t-sql的定義方法,賦值有兩種方法,定義時賦值,和先定義後賦值,定義游標像定義其他區域性變數一樣前面要加@,注意如果是全域性的游標,只支援定義時直接賦值,並且不能在游標前面加@

--定義時直接賦值

declare test_cursor cursor for

select * from dbo.tb1

--先定義後賦值

declare @test_cursor2 cursor

set @test_cursor2=cursor for

select * from dbo.tb2

local和global

local意味著游標的生命週期只在批處理或函式或儲存過程中可見,而global意味著游標對於特定的連線作為上下文,全域性有效

--定義後直接賦值(全域性)

declare test_cursor cursor global for

select * from dbo.tb1

--(區域性)

declare test_cursor2 cursor local for

select * from dbo.tb2

--用go結束上面的作用域

goopen test_cursor

open test_cursor2

報錯:test_cursor2不存在

注意:如果不指定游標作用域,預設為global

forward_only和scroll 二選一

forward_only意味著游標只能從資料集開始向著資料集結束的方向讀取,唯一選項fetch next,而scroll支援游標在定義資料集上向著任何方向或者任何位置移動

--不加引數預設為forward_only

declare test_cursor cursor for

select * from dbo.tb1

--加forward_only

declare test_cursor2 cursor forward_only for

select * from dbo.tb1

--加scroll

declare test_cursor3 cursor scroll for

select * from dbo.tb1

open test_cursor

open test_cursor2

open test_cursor3

fetch last from test_cursor

fetch last from test_cursor2

fetch last from test_cursor3--讀取最後一行

報錯:只進游標test_cursor不能與last一起使用,只進游標test_cursor2不能與last一起使用

static keyset dynamic 和 fast_forward四選一

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

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

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

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

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

read_only scroll_locks optimistic三選一

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

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

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

->開啟游標:open test_cursor 注意,當全域性游標和區域性游標變數重名時,缺省會開啟區域性變數游標

->使用游標:游標分為兩部分,一部分是操作游標在資料集內的指向,另一部分是將游標所直向的行的部分或全部內容進行操作

只支援6中移動選項,到第一行(first),最後一行(last),下一行(next),上一行(prior),直接跳到某一行(absolute(n)),相對於目前跳到幾行(relative(n))

--必須指定scroll否則只支援next只進選項

declare test_cursor scroll for

select * from dbo.tb1

open test_cursor

declare @c nvarchar(10)

--取下一行

fetch next from test_cursor into @c

print @c

--取最後一行

fetch last from test_cursor into @c

print @c

--取第一行

fetch first from test_cursor into @c

print @c

--取上一行

fetch prior from test_cursor into @c

print @c

--取第三行

fetch absolute 3 from test_cursor into @c

print @c

--取相對目前來說上一行(-2相對於目前來說向上移動2行,2對目前來說向下移動2行)

fetch relative -1 from test_cursor into @c

print @c

對於為指定scroll的,只能用next選項

游標經常回和全域性變數@@fetch_status 與whitle迴圈來共同使用,從來遍歷游標所在的資料集

declare test_cursor cursor scroll for

select id,name from dbo.tb1

open test_cursor

declare @id int

declare @name nvarchar(10)

while @@fetch_status=0

begin

print @id

print @name

fetch next from test_cursor into @id,@name

endclose test_cursor

deallocate test_cursor

->關閉游標: close test_cursor

->釋放游標: deallocate test_cursor

注釋1:

->游標的定義的複雜程度是和引數有關,而游標的引數設定是對游標的原理了解程度。

->游標的原理:游標是定義在特定資料集上的指標,我們控制這個指標遍歷資料集,或者僅僅是指向特定的行,所以游標是定義在以select開始的資料集上的。

注釋2:

如果能不用游標,盡量不要使用游標

用完用完之後一定要關閉和釋放

盡量不要在大量資料上定義游標

盡量不要使用游標上更新資料

盡量不要使用insensitive, static和keyset這些引數定義游標

如果可以,盡量使用fast_forward關鍵字定義游標

如果只對資料進行讀取,當讀取時只用到fetch next選項,則最好使用forward_only引數

sql server 中游標的使用

create table borrowbook 建立表學生借書 borrowbook int identity 1,1 stutid int stufeeid int borrowdate datetime,returndate datetime,fee money create table stu...

SQL Server中游標的使用

舉個栗子 臨時變數 declare iduniqueidentifier 宣告游標名 declare cursor name cursor forselect id from com datadictionaryinfo 開啟游標 open cursor name 先查詢一次再迴圈,防止有多個游標時...

SQL Server游標的使用

declare cursor name cursor local global forward only scroll static keyset dynamic fast forward read only scroll locks optimistic type warning for sele...