sql 执行报错,why? 帮我改改,谢谢

yanjianhong 2003-11-25 04:49:22

declare cursor_basic cursor scroll for select fact_id ,used from basic_db where used=0 for update of used,fact_id
fetch next from cursor_basic into @vchfact_id

while @@fetch_status = 0
begin
...........
end

close cursor_basic

服务器: 消息 16957,级别 16,状态 4,过程 tuili,行 29
[Microsoft][ODBC SQL Server Driver][SQL Server]在 READ ONLY 游标上不能指定 FOR UPDATE。
...全文
39 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuiniu 2003-11-25
  • 打赏
  • 举报
回复
-- =============================================
-- Declare a SCROLL cursor and use various FETCH options
-- =============================================
DECLARE <cursor_name, sysname, test_cursor> SCROLL CURSOR FOR
<select_statement, , SELECT * FROM pubs.dbo.authors>

OPEN <cursor_name, sysname, test_cursor>

-- Fetch the first row in the cursor.
FETCH FIRST FROM <cursor_name, sysname, test_cursor>

-- Fetch the last row in the cursor.
FETCH LAST FROM <cursor_name, sysname, test_cursor>

-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM <cursor_name, sysname, test_cursor>

-- Fetch the row immediately after the current row in the cursor.
FETCH NEXT FROM <cursor_name, sysname, test_cursor>

-- Fetch the nth row in the cursor.
FETCH ABSOLUTE <row_number, int, 8> FROM <cursor_name, sysname, test_cursor>

-- Fetch the row that is n rows before or after the current row.
FETCH RELATIVE <-/+ row_number, int, -2> FROM <cursor_name, sysname, test_cursor>

CLOSE <cursor_name, sysname, test_cursor>
DEALLOCATE <cursor_name, sysname, test_cursor>
GO

yanjianhong 2003-11-25
  • 打赏
  • 举报
回复
pengdali(大力 V3.0) :
谢谢,我想用滚动游标
pengdali 2003-11-25
  • 打赏
  • 举报
回复
declare cursor_basic cursor for
select fact_id ,used from basic_db where used=0 for update of used,fact_id


fetch next from cursor_basic into @vchfact_id

while @@fetch_status = 0
begin
...........
end

close cursor_basic
txlicenhe 2003-11-25
  • 打赏
  • 举报
回复
SQL2000的模板:

-- =============================================
-- Declare and using an UPDATE cursor
-- =============================================
DECLARE <@variable_1, sysname, @v1> <datatype_for_variable_1, sysname, varchar(20)>,
<@variable_2, sysname, @v2> <datatype_for_variable_2, sysname, varchar(40)>

DECLARE <cursor_name, sysname, test_cursor> CURSOR
FOR SELECT <column_1, sysname, au_fname>, <column_2, sysname, au_lname> FROM <table_name, sysname, pubs.dbo.authors>
FOR UPDATE of <column_1, sysname, au_fname>

DECLARE @count smallint
SELECT @count = 1

OPEN <cursor_name, sysname, test_cursor>
FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO <@variable_1, sysname, @v1>, <@variable_2, sysname, @v2>

WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
-- PRINT 'add user-defined code here...'
-- eg
PRINT 'updating record of ' + @v1 + ' ' + @v2
UPDATE pubs.dbo.authors
SET au_fname = @v1 + '-' + CAST(@count AS varchar(4))
WHERE au_lname = @v2
END
FETCH NEXT FROM <cursor_name, sysname, test_cursor> INTO <@variable_1, sysname, @v1>, <@variable_2, sysname, @v2>
SELECT @count = @count + 1
END

CLOSE <cursor_name, sysname, test_cursor>
DEALLOCATE <cursor_name, sysname, test_cursor>
GO

34,681

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧