使用游标更新 [英] Using Cursors Update

查看:93
本文介绍了使用游标更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要帮助微笑| :)

我有这样的桌子

itemcode itemiicode itemordercode序列号cpserialnum
4 2 4 21 21
5 2 5 -1 -1
6 3 6 14 14
7 4 7 15 15
8 5 8 41 41
9 6 9 72 72
10 6 10 -1 -1
11 6 11 -1 -1


我想要使​​用游标的下表的结果


itemcode itemiicode itemordercode序列号cpserialnum
4 2 4 21 21
5 2 5 -1 21
6 3 6 14 14
7 4 7 15 15
8 5 8 41 41
9 6 9 72 72
10 6 10 -1 72
11 6 11 -1 72


itemcode是主键
使用游标,我们必须更新表

请帮助:)
谢谢与问候
Harsha

hello guys, i need help Smile | :)

i have a table like this

itemcode itemiicode itemordercode serialnum cpserialnum
4 2 4 21 21
5 2 5 -1 -1
6 3 6 14 14
7 4 7 15 15
8 5 8 41 41
9 6 9 72 72
10 6 10 -1 -1
11 6 11 -1 -1


i want result like below table using Cursors


itemcode itemiicode itemordercode serialnum cpserialnum
4 2 4 21 21
5 2 5 -1 21
6 3 6 14 14
7 4 7 15 15
8 5 8 41 41
9 6 9 72 72
10 6 10 -1 72
11 6 11 -1 72


itemcode is the primary key
Using Cursors we have to update the table

Please Help :)
Thanks and Regards
Harsha

推荐答案

我不确定您在计算最后一列时采用了什么逻辑,但这是使用游标并更新表的示例:
I am not sure what logic you have put for calculating the last column, but here is an example of using the cursor and updating the table:
DECLARE @field1 int;
DECLARE @field3 int;
DECLARE c1 CURSOR FOR field1, field3 from table1 for update of field3;
open c1;
FETCH NEXT FROM c1 into @field1, @field3
WHILE @@FETCH_STATUS = 0
BEGIN
        -- add the logic you want to add..example
        SET @field3 = @field3 + 20
        UPDATE table1 set field3  = @field3 where current of c1
        FETCH NEXT FROM c1 into @field1, @field3

END
CLOSE c1;
DEALLOCATE c1;



如果可以,请尝试在没有光标的情况下执行此操作



try doing this without cursors if you can


这篇关于使用游标更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆