我认为我可以使用WHILE LOOP执行此操作,但每次都会更新所有行。
我有一个表,我需要使用应用程序唯一值函数更新具有唯一ID的两个字段。该函数给了我一个GUID(Trans),另一个给了我一个唯一的Trans_ID。我需要使用这些唯一ID更新每一行。
我将更新一行,运行该函数以获取下一个唯一数字,然后填充下一行。我无法弄清楚如何做到这一点。
DECLARE @RowCount as INT
DECLARE @IntFlag as INT
SET @IntFlag = 1
SELECT @RowCount = count(*) from trans_periodic_retainer_final
DECLARE @TransID as char(20)
DECLARE @BatchID uniqueidentifier
WHILE @IntFlag <=@RowCount
BEGIN
/* Functions to get the unique values */
UPDATE Increments SET Increment = Increment+1 WHERE v7Tables = 'Transactions' AND Site = 'TESTSERVER'
SET @BatchID = newid()
SELECT @TransID = Increment FROM Increments WHERE v7Tables = 'Transactions' AND Site = 'TESTSERVER'
UPDATE Transactions set trans = @BatchId, Trans_id = @TransID
SET @IntFlag = @intFlag +1
END
任何帮助将不胜感激。
答案 0 :(得分:0)
希望您尝试更新“交易表”。在这种情况下,你在update语句中缺少where子句,而且我还有一个问题,你可以在where子句中使用哪一列。?
UPDATE Transactions set trans = @BatchId, Trans_id = @TransID WHERE (IdentityColum) = Value.
答案 1 :(得分:0)
这是我现在能想到的,试着告诉我。
DECLARE Tran_Cursor CURSOR FOR
SELECT * FROM Transactions
OPEN Tran_Cursor
FETCH NEXT FROM Tran_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Increments SET Increment = Increment+1 WHERE v7Tables = 'Transactions' AND Site = 'TESTSERVER'
SET @BatchID = newid()
SELECT @TransID = Increment FROM Increments WHERE v7Tables = 'Transactions' AND Site = 'TESTSERVER'
UPDATE Transactions set trans = @BatchId, Trans_id = @TransID WHERE CURRENT OF Tran_Cursor
SET @IntFlag = @intFlag +1
FETCH NEXT FROM Tran_Cursor
END
CLOSE Tran_Cursor
DEALLOCATE Tran_Cursor
如果您不想更新已具有唯一ID的记录,则可以替换以下语句
DECLARE Tran_Cursor CURSOR FOR
SELECT * FROM Transactions
以下
DECLARE Tran_Cursor CURSOR FOR
SELECT * FROM Transactions WHERE Trans_id IS NOT NULL