Sql server 继续获取“游标为只读”
我的代码似乎很超前 我想用一个唯一的计数器更新一个特定的字段,该计数器不等于{1,2,3,…} 我不断收到错误“光标是只读的” 还有:有没有更简单的方法Sql server 继续获取“游标为只读”,sql-server,sql-server-2008-r2,cursor,sql-update,Sql Server,Sql Server 2008 R2,Cursor,Sql Update,我的代码似乎很超前 我想用一个唯一的计数器更新一个特定的字段,该计数器不等于{1,2,3,…} 我不断收到错误“光标是只读的” 还有:有没有更简单的方法 declare @MaxVal int = NULL declare @fetchVal int = NULL select @MaxVal = MAX(tp_Id)+1 from [<tableContainingInitialMaxval>] /** some default **/ DECLARE curs01 CURSO
declare @MaxVal int = NULL
declare @fetchVal int = NULL
select @MaxVal = MAX(tp_Id)+1 from [<tableContainingInitialMaxval>]
/** some default **/
DECLARE curs01 CURSOR
for select @maxVal + row_number() OVER (order by [<someUniqueField>]) from [<table2update>];
(used FOR UPDATE OF [<field2update>] but that made no difference)
open curs01
FETCH NEXT FROM curs01 INTO @fetchVal;
WHILE @@FETCH_STATUS = 0
begin
update [<table2update>] set [<field2update>] = @fetchVal
WHERE CURRENT OF curs01;
FETCH NEXT FROM curs01 INTO @fetchVal;
end;
CLOSE curs01;
DEALLOCATE curs01;
GO
这不需要光标
DECLARE @MaxVal INT = NULL
SELECT @MaxVal = MAX(tp_Id) + 1
FROM tableContainingInitialMaxval;
WITH CTE
AS (SELECT *,
@maxVal + row_number() OVER (ORDER BY someUniqueField) AS rn
FROM table2update)
UPDATE CTE
SET field2update = rn
FWIW,没有SQL Server 8。最新的版本是6.5、7.0、2000、2005、2008、2008 R2和2012。对不起,这是2008 R2。这看起来很干净,我会在14小时内回到工作岗位后立即通知你