sql - 光标说它是只读的,即使我声明它是 "for update"

标签 sql sql-server sql-server-2008

我正在尝试更新游标内的一行。我想要做的是用 OLD_QTY 和 NEW_QTY 更新一系列记录。但是,当我尝试进行更新时,它给出了错误 The cursor is READ ONLY即使我包含了 for update of OLD_QTY, NEW_QTY在我的声明中。如果我包含 OLD_QTY 没有区别和 NEW_QTY在选择语句中。

declare @current_inv_guid uniqueidentifier
declare @last_inv_guid uniqueidentifier
declare @current_vid int
declare @last_vid int
--declare @current_new_qty money
declare @last_new_qty money
--declare @current_old_qty money

declare iaCursor cursor 
      for select INV_GUID, old_VID
          --, OLD_QTY, NEW_QTY  
          from #IA 
          order by INV_GUID, old_vid, ENTRY_NUM 
      for update --of OLD_QTY, NEW_QTY
open iaCursor
Fetch next from iaCursor into @current_inv_guid, @current_vid --, @current_old_qty, @current_new_qty

while @@fetch_status = 0
begin
    --test to see if we hit a new chain.
    if(@last_inv_guid <> @current_inv_guid or @current_vid <> @last_vid)
    begin
        set @last_new_QTY = (select #lots.QTY_RECEIVED from #lots where #lots.INV_GUID = @current_inv_guid and LOT_VID = @current_vid)
        set @last_inv_guid = @current_inv_guid
        set @last_vid = @current_vid    
    end

    --update the current link in the chain
    update #ia
        set OLD_QTY = @last_new_QTY,
            NEW_QTY = @last_new_QTY + QTY_CHANGE,
            @last_new_QTY = @last_new_QTY + QTY_CHANGE
        where current of iaCursor

    --get the next link
    fetch next from iaCursor into @current_inv_guid, @current_vid --, @current_old_qty, @current_new_qty
end 

close iaCursor
deallocate iaCursor

最佳答案

放一个 order byselect使光标只读。

关于sql - 光标说它是只读的,即使我声明它是 "for update",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480727/

相关文章:

java - Java 的 SQL 解析器库 - 检索 SQL 语句中存在的表名列表

sql - 在 IBM netezza SQL 表中查找多列的不同组合时出错

sql-server - 我可以在连接字符串中设置隔离级别吗?

sql - Transact SQL 运行另一个 Transact SQL 脚本

sql - 转换为日期时间仅在 WHERE 子句上失败?

sql - 通过Apache Phoenix向表中添加列的默认值等于HBase中现有列的值

python - 我的 Python SQLite 代码是否容易受到 SQL 注入(inject)?

按星期几(或日期减 7)的 SQL 滚动平均值

sql - 对 BigQuery 中多列的数字求和

sql - 从表中选择不同的前 4 条记录