次のコードを機能させようとしています。重複がある場合にのみ TranID を更新したい
update Test set TranID=(select MAX(TranID)+1 from Test)
from Test
group by TranID
having COUNT(TranID)>1
次のコードを機能させようとしています。重複がある場合にのみ TranID を更新したい
update Test set TranID=(select MAX(TranID)+1 from Test)
from Test
group by TranID
having COUNT(TranID)>1
これを試して :
DECLARE @TranID INT = ( SELECT MAX(TranID)
FROM Test
);
WITH List
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY TranID ) + @TranID NewTranID ,
TranID
FROM Test T
WHERE T.TranID IN ( SELECT T2.TranID
FROM Test T2
GROUP BY T2.TranID
HAVING COUNT(T2.TranID) > 1 )
)
UPDATE List
SET TranID = NewTranID
私の頭の上から、これはうまくいくはずです:
UPDATE Test
SET TranID = (select MAX(TranID)+1 from Test)
FROM Test
inner JOIN (SELECT TranID
FROM Test
GROUP BY TranID
HAVING COUNT(*) > 1) d ON d.TranID = Test.TranID