sql游标,sql游标语法
SQL?数据库中的游标指的是什么,有什么作用?
游标:
作用:通常情况下,
关系数据库
中的操作总是对整个
记录
集产生影响,例如使用
SELECT语句
检索
数据表
时,将得到所有满足该
语句
where
子句
中
条件
的记录,而在实际应用
过程
中,经常需要
每次处理一条或者一部分记录。在这种情况下,需要使用游标在
内部
处理结果集合,他可
以有助于识别一个数据集合内部指定的记录,从而可以有选择的按记录执行操作。
Sql中的游标是干嘛的
在数据库中,游标提供了一种对从表中检索出的数据进行操作的灵活手段。就本质而言,游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。
游标总是与一条SQL 选择语句相关联因为游标由结果集(可以是零条、一条或由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成。
游标关于数据库中的操作会对整个行集产生影响。由?SELECT?语句返回的行集包括所有满足该语句?WHERE?子句中条件的行。由语句所返回的这一完整的行集被称为结果集。
应用程序,特别是交互式联机应用程序,并不总能将整个结果集作为一个单元来有效地处理。这些应用程序需要一种机制以便每次处理一行或一部分行。游标就是提供这种机制的结果集扩展。
扩展资料:
游标通过以下方式扩展结果处理:
1.允许定位在结果集的特定行。
2.从结果集的当前位置检索一行或多行。
3.支持对结果集中当前位置的行进行数据修改。
4.为由其他用户对显示在结果集中的数据库数据所做的更改提供不同级别的可见性支持。
5.提供脚本、存储过程和触发器中使用的访问结果集中的数据的?Transact-SQL?语句。
参考资料来源:百度百科—游标
SQL必知必会(游标)
第一步,定义游标。
第二步,打开游标。
第三步,从游标中取得数据。
第四步,关闭游标。
最后一步,释放游标。
你会发现执行 call calc_hp_max() 这一句的时候系统会提示 1329 错误,也就是在 LOOP 中当游标没有取到数据时会报的错误。
使用游标来解决一些常见的问题
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游标怎么用
例子
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 游标 是什么意思
游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果。每个游标区都有一个名字。用户可以用SQL语句逐一从游标中获取记录,并赋给主变量,交由主语言进一步处理。