sql游標使用實例
『壹』 sql游標怎麼用
例子
table1結構如下
id
int
name
varchar(50)
declare
@id
int
declare
@name
varchar(50)
declare
cursor1
cursor
for
--定義游標cursor1
select
*
from
table1
--使用游標的對象(跟據需要填入select文)
open
cursor1
--打開游標
fetch
next
from
cursor1
into
@id,@name
--將游標向下移1行,獲取的數據放入之前定義的變數@id,@name中
while
@@fetch_status=0
--判斷是否成功獲取數據
begin
update
table1
set
name=name+'1'
where
id=@id
--進行相應處理(跟據需要填入SQL文)
fetch
next
from
cursor1
into
@id,@name
--將游標向下移1行
end
close
cursor1
--關閉游標
deallocate
cursor1
『貳』 sql游標實例!
這是模板。
-- =============================================
-- Create procere with CURSOR OUTPUT Parameters
-- =============================================
-- drop the store procere if it already exists
IF EXISTS (SELECT name
FROM sysobjects
WHERE name = N'<procere_name, sysname, proc_test>'
AND type = 'P')
DROP PROCEDURE <procere_name, sysname, proc_test>
GO
-- create the store procere
CREATE PROCEDURE <procere_name, sysname, proc_test>
<@proc_cursor_name, , @proc_test_cursor> CURSOR VARYING OUTPUT
AS
SET <@proc_cursor_name, , @proc_test_cursor> = CURSOR FOR
<select_statement, , select 1>
OPEN <@proc_cursor_name, , @proc_test_cursor>
GO
-- =============================================
-- example to execute the store procere
-- =============================================
DECLARE <@variable_cursor_name, , @test_cursor_variable> CURSOR
EXEC <procere_name, sysname, proc_test> <@proc_cursor_name, , @proc_test_cursor> = <@variable_cursor_name, , @test_cursor_variable> OUTPUT
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM <@variable_cursor_name, , @test_cursor_variable>
PRINT 'put user defined code here'
END
CLOSE <@variable_cursor_name, , @test_cursor_variable>
DEALLOCATE <@variable_cursor_name, , @test_cursor_variable>
GO
『叄』 sql 中游標的作用及使用方法
游標可以從資料庫中查詢出一個結果集,在你關閉它之前,你可以反復使用這個結果集,讀取這個結果集中的任意行任意欄位的內容,一般在存儲過程或前台程序中常見。
『肆』 SQL中游標的應用
begin
open c1;
fetch c1 into vtest;
while c1%found loop
update im_test1 set test3=vtest; //這里不加where條件是更新所有行???
fetch c1 into vtest;
end loop;
end;
close c1;
『伍』 求一個SQL Server游標應用實例
--申明一個游標
DECLARE MyCursor CURSOR
FOR SELECT TOP 5 FBookName,FBookCoding FROM TBookInfo
--打開一個游標
OPEN MyCursor
--循環一個游標
DECLARE @BookName nvarchar(2000),@BookCoding nvarchar(2000)
FETCH NEXT FROM MyCursor INTO @BookName,@BookCoding
WHILE @@FETCH_STATUS =0
BEGIN
print 'name'+@BookName
FETCH NEXT FROM MyCursor INTO @BookName,@BookCoding
END
--關閉游標
CLOSE MyCursor
--釋放資源
DEALLOCATE MyCursor
游標屬於行級操作 消耗很大
SQL查詢是基於數據集的
所以一般查詢能有 能用數據集 就用數據集 別用游標
數據量大 是性能殺手
『陸』 sql如何使用游標查詢指定行記錄
--用游標
DECLARE @COLUMN1 VARCHAR(10)
DECLARE @LineNum INT
SET @LineNum = 1
DECLARE CUR_TEST CURSOR FOR
SELECT 語句
OPEN CUR_TEST
FETCH NEXT FROM CUR_TEST INTO @COLUMN1,...
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@LineNum >= N and @LineNum <= M)
BEGIN
SELECT @COLUMN1,...
END
SET @LineNum = @LineNum + 1
END
CLOSE CUR_TEST
DEALLOCATE CUR_TEST
--用雙TOP直接取出,從前M條倒序取出前(M-N)條,等同於取出了N到M之間的
SELECT TOP (M-N) * FROM (SELECT TOP M * FROM 表名 WHERE 語句 ORDER BY COLUMNname DESC)
『柒』 SQL游標如何使用
A. 在簡單的游標中使用 FETCH
下例為 authors 表中姓以字母 B 開頭的行聲明了一個簡單的游標,並使用 FETCH NEXT 逐個提取這些行。FETCH 語句以單行結果集形式返回由 DECLARE CURSOR 指定的列的值。
USE pubs
GO
DECLARE authors_cursor CURSOR FOR
SELECT au_lname FROM authors
WHERE au_lname LIKE "B%"
ORDER BY au_lname
OPEN authors_cursor
-- Perform the first fetch.
FETCH NEXT FROM authors_cursor
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
au_lname
----------------------------------------
Bennet
au_lname
----------------------------------------
Blotchet-Halls
au_lname
----------------------------------------
B. 使用 FETCH 將值存入變數
下例與上例相似,但 FETCH 語句的輸出存儲於局部變數而不是直接返回給客戶端。PRINT 語句將變數組合成單一字元串並將其返回到客戶端。
USE pubs
GO
-- Declare the variables to store the values returned by FETCH.
DECLARE @au_lname varchar(40), @au_fname varchar(20)
DECLARE authors_cursor CURSOR FOR
SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE "B%"
ORDER BY au_lname, au_fname
OPEN authors_cursor
-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.
FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- Concatenate and display the current values in the variables.
PRINT "Author: " + @au_fname + " " + @au_lname
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
INTO @au_lname, @au_fname
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
Author: Abraham Bennet
Author: Reginald Blotchet-Halls
C. 聲明 SCROLL 游標並使用其它 FETCH 選項
下例創建一個 SCROLL 游標,使其通過 LAST、PRIOR、RELATIVE 和 ABSOLUTE 選項支持所有滾動能力。
USE pubs
GO
-- Execute the SELECT statement alone to show the
-- full result set that is used by the cursor.
SELECT au_lname, au_fname FROM authors
ORDER BY au_lname, au_fname
-- Declare the cursor.
DECLARE authors_cursor SCROLL CURSOR FOR
SELECT au_lname, au_fname FROM authors
ORDER BY au_lname, au_fname
OPEN authors_cursor
-- Fetch the last row in the cursor.
FETCH LAST FROM authors_cursor
-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM authors_cursor
-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM authors_cursor
-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM authors_cursor
-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM authors_cursor
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
au_lname au_fname
---------------------------------------- --------------------
Bennet Abraham
Blotchet-Halls Reginald
Carson Cheryl
DeFrance Michel
del Castillo Innes
Dull Ann
Green Marjorie
Greene Morningstar
Gringlesby Burt
Hunter Sheryl
Karsen Livia
Locksley Charlene
MacFeather Stearns
McBadden Heather
O'Leary Michael
Panteley Sylvia
Ringer Albert
Ringer Anne
Smith Meander
Straight Dean
Stringer Dirk
White Johnson
Yokomoto Akiko
au_lname au_fname
---------------------------------------- --------------------
Yokomoto Akiko
au_lname au_fname
---------------------------------------- --------------------
White Johnson
au_lname au_fname
---------------------------------------- --------------------
Blotchet-Halls Reginald
au_lname au_fname
---------------------------------------- --------------------
del Castillo Innes
au_lname au_fname
---------------------------------------- --------------------
Carson Cheryl
『捌』 sql語句編寫存儲過程,使用游標循環列印學生表中的數據,求大神
寫一個例子給樓主看下就知道了:
在sqlserver2000中新建一個存儲過程:
CREATEPROCEDUREPK_Test
AS
//聲明1個變數
declare@namenvarchar(20)
//聲明一個游標mycursor,select語句中參數的個數必須要和從游標取出的變數名相同
//打開游標
openmycursor
//從游標里取出數據賦值到我們剛才聲明的2個變數中
fetchnextfrommycursorinto@name
//判斷游標的狀態
//0fetch語句成功
//-1fetch語句失敗或此行不在結果集中
//-2被提取的行不存在
while(@@fetch_status=0)
begin
//顯示出我們每次用游標取出的值
print'游標成功取出一條數據'
print@name
//用游標去取下一條記錄
fetchnextfrommycursorinto@name
end
//關閉游標
closemycursor
//撤銷游標
deallocatemycursor
GO
『玖』 sql游標的寫法
給你一個游標的寫法。
此demo的目地是將游標行的name更新到另一張表的欄位中條件是2張表的ID相同
Declare@Idvarchar(20)
Declare@Namevarchar(20)
DeclareCurCursorFor
selectid,namefromtemp1
OpenCur
FetchnextFromCurInto@Id,@Name
While@@fetch_status=0
Begin
UpdatetempSet[c3]=@Namewhere[id]=@Id
FetchNextFromCurInto@Id,@Name
End
CloseCur
DeallocateCur
『拾』 sql server 中游標的作用麻煩簡單舉例說明。
游標說簡單點都是設置一個數據表的行指針,然後使用循環等操作數據
以下是一個示例
createprocereUpdateValue--存儲過程裡面放置游標
as
begin
declareUpdateCursorcursor--聲明一個游標,查詢滿足條件的數據
forselect主鍵,SD_VALfromEQ_SD_D
openUpdateCursor--打開
declare@idint,@SD_VALnvarchar(20)--聲明一個變數,用於讀取游標中的值
fetchnextfromUpdateCursorinto@id,@SD_VAL
while@@fetch_status=0--循環讀取
begin
updateEQ_SD_Dsetname=@SD_VALwhereid=@id
fetchnextfromUpdateCursorinto@id,@SD_VAL
end
closeUpdateCursor--關閉
deallocateUpdateCursor--刪除
end
這里是一個教學
http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html