Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Only insert records where the new value differs from the current value RE: Only insert records where the new value differs from the current value

  • To make sure I'm reading it correctly, you want to insert rows from UserRank into UserRankHistory that don't already appear in UserRankHistory. If I'm interpreting it correctly, something along these lines should give you what you're after:

    WITH cteNewRows AS (

    SELECT UserID, RankName

    FROM dbo.UserRanks

    EXCEPT

    SELECT UserID, RankName

    FROM dbo.UserRankHistory

    )

    INSERT INTO dbo.UserRankHistory(UserID, RankName, CreateDate)

    SELECT UserID, RankName, GETDATE()

    FROM cteNewRows;

    This is pretty simple, but because I don't have table DDL and sample data, I have to call it untested. Hope this helps.