SQL游标

本文介绍了SQL游标的三种使用方法:在简单游标中使用FETCH逐行提取数据;使用FETCH将值存入变量;声明SCROLL游标并使用其他FETCH选项进行数据滚动操作。
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

为您推荐:

2008-07-31 22:13 人生开始于思想 | 二级 最快回答
我就用通俗的话和你说吧。。。
其实游标就是一个指向一个结果集的东西,你可以用你的游标在这个结果集上面任意的操作,当然,游标也有不同的游标,有的是只可以浏览不能修改,有的是只可以重上往下浏览,具体的参数在sql的连机文档中详看
SQL 游标是一种数据库对象,用于在结果中逐处理数据。它主要用于需要对每一数据进单独操作的场景,尤其是在存储过程或触发器中。 ### 使用方法 1. **声明游标** 在声明游标时,需要指定一个查询语句作为游标的数据源。例如: ```sql DECLARE cursor_name CURSOR FOR SELECT column1, column2 FROM table_name WHERE condition; ``` 2. **打开游标** 打开游标后,数据库会执游标关联的查询,结果准备好以供遍历。 ```sql OPEN cursor_name; ``` 3. **获取数据** 使用 `FETCH` 语句游标中检索当前的数据,移动游标指针到下一。通常与循环结合使用,以处理所有。 ```sql FETCH NEXT FROM cursor_name INTO @variable1, @variable2; ``` 4. **关闭游标** 当完成对数据的操作后,需要关闭游标以释放资源。 ```sql CLOSE cursor_name; ``` 5. **释放游标(可选)** 在某些数据库系统中,可以使用 `DEALLOCATE` 来完全移除游标定义。 ```sql DEALLOCATE cursor_name; ``` ### 示例代码 以下是一个完整的 SQL 游标示例,假设我们有一个名为 `Employees` 的表,包含员工信息: ```sql -- 声明变量 DECLARE @EmployeeID INT; DECLARE @Name NVARCHAR(100); -- 声明游标 DECLARE EmployeeCursor CURSOR FOR SELECT EmployeeID, Name FROM Employees; -- 打开游标 OPEN EmployeeCursor; -- 获取第一数据 FETCH NEXT FROM EmployeeCursor INTO @EmployeeID, @Name; -- 遍历游标 WHILE @@FETCH_STATUS = 0 BEGIN -- 处理每一数据,这里仅输出员工信息 PRINT 'Employee ID: ' + CAST(@EmployeeID AS NVARCHAR) + ', Name: ' + @Name; -- 获取下一数据 FETCH NEXT FROM EmployeeCursor INTO @EmployeeID, @Name; END -- 关闭游标 CLOSE EmployeeCursor; -- 释放游标 DEALLOCATE EmployeeCursor; ``` ### 注意事项 - **性能问题**:游标是面向的操作,相比合操作效率较低,因此在处理大数据量时应谨慎使用[^4]。 - **事务管理**:如果在事务中使用游标,需要注意事务的提交和回滚对游标的影响。 - **兼容性**:不同数据库系统(如 MySQL、PostgreSQLSQL Server)对游标的实现略有差异,需根据具体系统调整语法。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值