Skip to content
 

Using the Merge Statement’s OUTPUT Clause to Match Inserted IDENTITY Fields with Source Table Fields

You are currently browsing comments. If you would like to return to the full story, you can read the full entry here: “Using the Merge Statement’s OUTPUT Clause to Match Inserted IDENTITY Fields with Source Table Fields”.

5 Comments

  1. Vincent D'Souza says:

    Here is the simplied version of that… enjoy…

    CREATE TABLE #MatchIDs
    (
    UpdateAction varchar(50),
    Table_1_EmployeeID int,
    Table_2_EmployeeID int
    )

    MERGE INTO Table_1 AS TARGET
    USING Table_2 AS SOURCE
    ON TARGET.id = SOURCE.id
    WHEN MATCHED THEN
    UPDATE SET TARGET.FristName = SOURCE.FIRSTNAME
    , TARGET.LASTNAME = SOURCE.LASTNAME
    WHEN NOT MATCHED THEN
    INSERT (FirstName, LASTNAME)
    VALUES (SOURCE.FIRSTNAME, SOURCE.LASTNAME)
    OUTPUT $action, t2.EmployeeID ,t1.EmployeeID into #MatchIDs;

  2. Varun Varghese says:

    I tried this, but I am getting the following error: Msg 4104, Level 16, State 1, Line 30
    The multi-part identifier “t1.EmployeeID” could not be bound.

    Any ideas what could be wrong. Thanks in advance.

  3. fotis says:

    briliant piece of code!

  4. Ben says:

    Hi,

    I really need to do this but am using SQL 2005 – is there any way that this can be done?

post a comment OR Post Your Question on our ASK! Community!