新聞中心
NEWS
當前位置: 首頁(yè) > 微信開(kāi)發(fā)
詳解SQL游標的用法
這篇文章主要介紹了SQL游標的詳解用法,文中講解非常詳細,游用法配合代碼幫助大家更好的詳解理解學(xué)習,感興趣的游用法朋友可以了解下
類(lèi)型:
1.普通游標 只有NEXT操作(zuo)
2.滾動(dòng)游標 有多種操作
1.普通游標
DECLARE @username varchar(20),@UserId varchar(100)
DECLARE cursor(′?`)_name CURSOR FOR --定義游標
SELECT TOP 10 UserId,UserName FROM UserIn┐(′ー`)┌fo
ORDER BY UserId DESC
OP??EN cursor_name --打開(kāi)游(′;д;`)標
FETCH NEXT FROM cursor_name?? INTO @UserId,@usern(′▽?zhuān)?)ame --抓取下一行游標數據
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT '用(yong)戶(hù)ID:'+@UserId+' '+'用戶(hù)名:'+@username
FETCH NEXT FROM cursor_name INTO @UserId,@username
END
CLOSE cursor_name --關(guān)閉游標
DEALLOCATE cursor_n??ame --釋放游標
結果:
用戶(hù)ID:zhizhi 用戶(hù)名:鄧鴻芝
用戶(hù)ID:yujie 用戶(hù)名:李玉杰
用戶(hù)ID:yuanyuaˉ\_(ツ)_/ˉn 用戶(hù)名:王夢(mèng)緣
用戶(hù)ID:yiyiren 用戶(hù)名:任毅
用戶(hù)ID:yanbo 用戶(hù)名:王艷波
用戶(hù)ID:xuxu 用戶(hù)名:陳佳緒
用戶(hù)ID:??xiangxiang 用戶(hù)名:李慶祥
用戶(hù)ID:wenwen 用戶(hù)名:魏文文
2.滾動(dòng)游標
--帶SCROLL選項的游標
SET NOCOUNT ON
DECLARE C SCROLL CURSOR FOR --SCORLL 后,有了更多的詳解┐(′?`)┌游標操作(滾動(dòng)游標)
SELECT TOP 10 UserId,UserName FROM UserInfo
ORDER BY UserId DESC
open="open" C
FETCH LAST FROM C --最后一行的數據,并將當前行為指定行
FETCH ABSOLUTE 4 FROM C --從第一行開(kāi)始的游用法第4行??數據,并將當前行為指定行 這里的詳解n可正可負,n>0 往下翻,游用法n<??;0 往上翻
FETCH RELATIVE 3 FROM C --相對于當前行的詳解后3行數據,并將當前行為指定??(ding)行 這里??的游用法n可正可負
FETC??H RELATIV(′?`*)E -2 FROM C --相對于當前行的前2行數據,并將當前行為(wei)指定行
FETCH PRIOR FROM C ----相對于當前行的詳解前1行數據
FE(?????)TCH FI??RST FR(╯°□°)╯︵ ┻━┻OM C --剛開(kāi)始第一行的數據,并將當前行為指(′;ω;`)定行
FETCH NEXT FROM C --相對于當前行的游用法后1行數據
CLOSE C
DEALLOCATE C
結果(可以參考第一個(gè)結果分析):
具體FETCH用法:
FETCH
[ [ NEXT | PRIOR | FIRST | LAST
| ABSOLUTE { n | @nvaヾ(′▽?zhuān)??r }
| REL??ATIVE { n | @nvar }
]
FROM
]
{ { [ GLOBAL ] cursor_name(?Д?) } | @cursor_variable_name }
[ INTO @variable_name [ ,...n ] ]
Arguments??
NEXT
Returns the result row immediately following the current row and increments the current row to the row returned. If FETCH NEXT is the(′?_?`) first fetch against a cursor, it?? returns the first row in the result set. NEXT is the default cursor fetch option.
PRIOR
Returns the result row immedia(?Д?)tely preceding(?????) the current row, and decrements the current row to the row returne(°□°)d. If FETCH PRIOR is the first fetch against a cursor,?? no row is returned and the cursor is left positioned before the first row.
FIRST
Returns the firs??t row in the cursor and makes it the current row.
LAST
Returns the last row in the cursor and makes it the current row.
ABSOLUTE { n| @nvar}
If n or @nvar is positive,??? returns the row n rows from the front of the cursor and ma(′;ω;`)kes the returned row the new current row. If n or @nvar is negative, returns the row n rows before the end of the cursor and maヾ(′?`)?kes the returned row the new current row. If n or @nvar is 0, no roヽ(′ー`)ノws are returned. n must be an integer constant and @nvar must be smallint, tinyint, or int.
RELATIVE { n| @nvar}
GLOBAL
Specifies that cursor_name refers to a global cursor.
cursor_name
Is the name of the open='open' cursor from which the fetch should?? be made. If both a global and a local cursor ex??ist with cursor_name as their name, cursor_name to the global cursor if GLOBAL is spec??ified and to the local cursor if GLOBA??L is not spec(/ω\)ified.
@cursor_variable_name
INTO @variable_name[ ,...ヾ(′▽?zhuān)??n]
Allows data from?? the columns of a fetch to be placed into local variables. Each variable in the list, from left to right, is associated with the corresponding column in the cursor result set. The data type of each variable must either match or be a supported implicit conversion of the data type of the corresponding result set column. The number of variables must match the number of columns in the cursor select liヾ(′▽?zhuān)??st.
以上就是詳解SQL游標的用法的詳細內容,更多關(guān)于SQL游標用法的詳解資料請關(guān)注腳本之家其它相關(guān)文章!
游用法來(lái)源:腳本之家
游用法鏈接:https://www.jb51.net/article/188053.htm
游用法客服電話(huà)18163829114
Copyright ? 2012-2018 天津九安特機電工程有限公司 版權所有 備案號:
客服電話(huà)13302579283