sqlserver游标使用
转自http://www.voidcn.com/article/p-xyiuurkl-uk.html 整理一下,关于游标,MSDN有: 过 Transact-SQL 服务器游标检索特定行。 Transact-SQL 语法约定
紧跟当前行返回结果行,并且当前行递增为返回行。如果 FETCH NEXT 为对游标的第一次提取操作,则返回结果集中的第一行。NEXT 为默认的游标提取选项。 PRIOR返回紧邻当前行前面的结果行,并且当前行递减为返回行。如果 FETCH PRIOR 为对游标的第一次提取操作,则没有行返回并且游标置于第一行之前。 FIRST返回游标中的第一行并将其作为当前行。 LAST返回游标中的最后一行并将其作为当前行。 ABSOLUTE {? n?|? @ nvar}如果?n?或?@nvar?为正,则返回从游标头开始向后的第?n?行,并将返回行变成新的当前行。如果?n?或@nvar?为负,则返回从游标末尾开始向前的第?n?行,并将返回行变成新的当前行。如果?n?或?@nvar?为 0,则不返回行。n?必须是整数常量,并且?@nvar?的数据类型必须为?smallint、tinyint?或?int。 RELATIVE {? n?|? @ nvar}如果?n?或?@nvar?为正,则返回从当前行开始向后的第?n?行,并将返回行变成新的当前行。如果?n?或@nvar?为负,则返回从当前行开始向前的第?n?行,并将返回行变成新的当前行。如果?n?或?@nvar?为 0,则返回当前行。在对游标进行第一次提取时,如果在将?n?或?@nvar?设置为负数或 0 的情况下指定 FETCH RELATIVE,则不返回行。n?必须是整数常量,@nvar?的数据类型必须为?smallint、tinyint?或?int。 GLOBAL指定?cursor_name?是指全局游标。 cursor_name要从中进行提取的打开的游标的名称。如果全局游标和局部游标都使用?cursor_name?作为它们的名称,那么指定 GLOBAL 时,cursor_name?指的是全局游标;未指定 GLOBAL 时,cursor_name?指的是局部游标。 @? cursor_variable_name游标变量名,引用要从中进行提取操作的打开的游标。 INTO? @ variable_name[? , ...n]允许将提取操作的列数据放到局部变量中。列表中的各个变量从左到右与游标结果集中的相应列相关联。各变量的数据类型必须与相应的结果集列的数据类型匹配,或是结果集列数据类型所支持的隐式转换。变量的数目必须与游标选择列表中的列数一致。 如果 SCROLL 选项未在 ISO 样式的 DECLARE CURSOR 语句中指定,则 NEXT 是唯一支持的 FETCH 选项。如果在 ISO 样式的 DECLARE CURSOR 语句中指定了 SCROLL 选项,则支持所有 FETCH 选项。 如果使用 Transact-SQL DECLARE 游标扩展插件,则应用下列规则:
@@FETCH_STATUS 函数报告上一个 FETCH 语句的状态。相同的信息记录在由?sp_describe_cursor?返回的游标中的?fetch_status?列中。这些状态信息应该用于在对由 FETCH 语句返回的数据进行任何操作之前,以确定这些数据的有效性。有关详细信息,请参阅?@@FETCH_STATUS (Transact-SQL)。 FETCH 权限默认授予任何有效的用户。 ? A. 在简单的游标中使用 FETCH以下示例为? USE AdventureWorks GO DECLARE contact_cursor CURSOR FOR SELECT LastName FROM Person.Contact WHERE LastName LIKE 'B%' ORDER BY LastName OPEN contact_cursor -- Perform the first fetch. FETCH NEXT FROM contact_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 contact_cursor END CLOSE contact_cursor DEALLOCATE contact_cursor GO B. 使用 FETCH 将值存入变量以下示例与示例 A 相似,但? USE AdventureWorks GO -- Declare the variables to store the values returned by FETCH. DECLARE @LastName varchar(50),@FirstName varchar(50) DECLARE contact_cursor CURSOR FOR SELECT LastName,FirstName FROM Person.Contact WHERE LastName LIKE 'B%' ORDER BY LastName,FirstName OPEN contact_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 contact_cursor INTO @LastName,@FirstName -- 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 'Contact Name: ' + @FirstName + ' ' + @LastName -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM contact_cursor INTO @LastName,@FirstName END CLOSE contact_cursor DEALLOCATE contact_cursor GO C. 声明 SCROLL 游标并使用其他 FETCH 选项以下示例创建一个? 。 另外,再举一个简单的例子: Declare @Id varchar(20)?Declare @Name varchar(20)? Declare Cur Cursor For? select substring(id,7) as id,name from temp1? Open Cur? Fetch next From Cur Into @Id,@Name? While @@fetch_status=0? Begin? Update temp Set [c3]=@Name where [id] like @Id+'%'? Fetch Next From Cur Into @Id,@Name? End? Close Cur? Deallocate Cur? ? 简单的FOR循环等: declare ? @i ? int ?? USE AdventureWorks GO -- Execute the SELECT statement alone to show the -- full result set that is used by the cursor. SELECT LastName,FirstName FROM Person.Contact ORDER BY LastName,FirstName -- Declare the cursor. DECLARE contact_cursor SCROLL CURSOR FOR SELECT LastName,FirstName OPEN contact_cursor -- Fetch the last row in the cursor. FETCH LAST FROM contact_cursor -- Fetch the row immediately prior to the current row in the cursor. FETCH PRIOR FROM contact_cursor -- Fetch the second row in the cursor. FETCH ABSOLUTE 2 FROM contact_cursor -- Fetch the row that is three rows after the current row. FETCH RELATIVE 3 FROM contact_cursor -- Fetch the row that is two rows prior to the current row. FETCH RELATIVE -2 FROM contact_cursor CLOSE contact_cursor DEALLOCATE contact_cursor GO
FETCH ??????????[ [ NEXT | PRIOR | FIRST | LAST ????????????????????| ABSOLUTE { n | @nvar } ????????????????????| RELATIVE { n | @nvar } ???????????????] ???????????????FROM ??????????] { { [ GLOBAL ] cursor_name } | @cursor_variable_name } [ INTO @variable_name [,...n ] ] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |