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”.
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”.
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;
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.
briliant piece of code!
Hi,
I really need to do this but am using SQL 2005 – is there any way that this can be done?
Hi Ben, sorry for the late reply. Unfortunately I don’t think there is.. At least that I know of.