I want to be able to increment the int value of a column returned in a result set of multiple rows by 1. for instance the following query will return a result set from a table that looks like the following:
我希望能够将多行结果集中返回的列的int值递增1.例如,以下查询将从表中返回如下所示的结果集:
select numinbatch from items i where i.ID > @itemID and i.BatchID = @batchID
4
5
6
7
I want to update these values by incrementing them by 1 so that:
我想通过将它们递增1来更新这些值,以便:
5
6
7
8
I have the following for now but Im getting a red squiggly error line under the plus sign.
我现在有以下内容,但我在加号下得到一个红色的波浪形错误线。
declare @idToIncrement as int
declare cur cursor fast_forward for select numinbatch from items i where i.ID > @itemID and i.BatchID = @batchID
open cur
fetch next from cur into @idToIncrement
while @@FETCH_STATUS = 0
Begin
EXEC
--increment numinbatch or @idToIncrement
@idToIncrement= @idToIncrement + 1
fetch next from cur into @field1, @field2
END
close cur
deallocate cur
I know that cursors are bad for performance so if any one has a better solution feel free to share.
我知道游标对性能不利,所以如果有任何人有更好的解决方案,请随意分享。
2 个解决方案
#1
4
I would do it like this:
我会这样做:
select numinbatch + 1 numinbatch_increased_by_1
from items i
where i.ID > @itemID and i.BatchID = @batchID
If you want to UPDATE
the values into the table, then
如果要将值更新到表中,那么
UPDATE items i
SET numinbatch = numinbatch + 1
WHERE i.ID > @itemID AND i.BatchID = @batchID
#2
1
SELECT numinbatch + 1 FROM items WHERE ID > @itemID AND BatchID = @batchID
assuming numinbatch is numeric. There's no need for a cursor.
假设numinbatch是数字。不需要游标。
#1
4
I would do it like this:
我会这样做:
select numinbatch + 1 numinbatch_increased_by_1
from items i
where i.ID > @itemID and i.BatchID = @batchID
If you want to UPDATE
the values into the table, then
如果要将值更新到表中,那么
UPDATE items i
SET numinbatch = numinbatch + 1
WHERE i.ID > @itemID AND i.BatchID = @batchID
#2
1
SELECT numinbatch + 1 FROM items WHERE ID > @itemID AND BatchID = @batchID
assuming numinbatch is numeric. There's no need for a cursor.
假设numinbatch是数字。不需要游标。