SQL Server中游標的使用

2022-03-02 16:53:56 字數 633 閱讀 4250

舉個栗子:

--

臨時變數

declare

@iduniqueidentifier

--宣告游標名

declare cursor_name cursor

forselect id from

com_datadictionaryinfo

--開啟游標

open

cursor_name

--先查詢一次再迴圈,防止有多個游標時@@fetch_status=-1不能進入下個游標迴圈的情況

fetch

next

from cursor_name into

@id--

迴圈取資料

while

@@fetch_status=0

begin

select

@idfetch

next

from cursor_name into

@idend

--關閉游標

close

cursor_name

--刪除游標

deallocate cursor_name

不過,現在比較少使用游標了,效率低。

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 中游標的使用

sql server 中游標的使用 1.游標是行讀取,占用資源比sql多 2.游標的使用情景 現存的系統中使用的是游標,查詢必須通過游標來實現 用盡了while 子查詢臨時表 表變數 自定義函式以及其他方式仍然無法實現的時候,使用游標 3.t sql 中游標的生命週期由5部分組成 定義游標 游標的定...

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...