declare @id nvarchar(20)
DECLARE My_Cursor CURSOR --定义游标
FOR (select autoid from U_VoucherItems where CardNum='k006' and CardSection='B') --查出需要的集合放到游标中
OPEN My_Cursor; --打开游标
FETCH NEXT FROM My_Cursor INTO @id; --读取第一行数据
WHILE @@FETCH_STATUS = 0
BEGIN
update U_VoucherItems set CardItemNum=(select MAX(CardItemNum)+1 from U_VoucherItems where CardNum='k006' and CardSection='B'
) where CardNum='k006' and autoid=@id
FETCH NEXT FROM My_Cursor INTO @id; --读取第一行数据
END
CLOSE My_Cursor; --关闭游标
DEALLOCATE My_Cursor; --释放游标
来源:https://www.cnblogs.com/su-king/p/5121866.html