我想更新光标内的一行。我正在尝试的是使用OLD_QTY和NEW_QTY更新记录链。但是,当我尝试执行更新时,即使在我的declration中包含了for update of OLD_QTY, NEW_QTY
,也会给出错误The cursor is READ ONLY
。如果我在select语句中包含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
相关:http://stackoverflow.com/questions/14172501/why-an-cursor-opened-for-a-select-with-order-by-does-not-reflect-updates-to-the – bummi