gpt4 book ai didi

sql-server - 继续获取 'cursor is READ-ONLY'

转载 作者:行者123 更新时间:2023-12-02 23:04:51 24 4
gpt4 key购买 nike

我的代码看起来很先进。

我想使用唯一计数器更新特定字段,该计数器不等于 {1,2,3,...}。

我不断收到错误“光标为只读”。

还有:有没有更简单的方法?

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

关于sql-server - 继续获取 'cursor is READ-ONLY',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8822145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com