2

次のコードを機能させようとしています。重複がある場合にのみ TranID を更新したい

update Test set TranID=(select MAX(TranID)+1 from Test)
from Test
group by TranID
having COUNT(TranID)>1
4

3 に答える 3

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
于 2013-05-10T20:00:47.487 に答える
0

私の頭の上から、これはうまくいくはずです:

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
于 2013-05-10T19:32:47.827 に答える