Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MERGE INTO with Scope_IDENTITY

When Merge into does an insert with the following statement, Scope_Identity returns the correct surrogate key information. However when an update is performed both Scope_Identity and @@Identity return the next available surrogate key. And when I added the output, I get a null on both update and insert.

How do I return the surrogate key on both the update and the insert?

DECLARE @Surrogate_KEY bigint


MERGE INTO [dbo].[MyTable] ChangeSet
USING (SELECT   @NaturalKey1 AS NaturalKey1, 
                @NaturalKey2 AS NaturalKey2, 
                @NaturalKey3 AS NaturalKey3,
                @Surrogate_KEY AS Surrogate_KEY) CurrentSet
ON  ChangeSet.NaturalKey1 = CurrentSet.NaturalKey1 AND 
    ChangeSet.NaturalKey2 = CurrentSet.NaturalKey2 AND 
    ChangeSet.NaturalKey3 = CurrentSet.NaturalKey3      
WHEN MATCHED THEN 
    UPDATE SET blah, blah, blah 

WHEN NOT MATCHED 
    THEN INSERT VALUES
       (
        blah, blah, blah
       )

output CurrentSet.*, @Surrogate_KEY ;

 print @Surrogate_KEY
 print @@IDENTITY
 print SCOPE_IDENTITY() 
like image 835
KenL Avatar asked May 27 '11 13:05

KenL


People also ask

Can we use CTE in Merge statement?

You can also use a CTE in a CREATE a view, as part of the view's SELECT query. In addition, as of SQL Server 2008, you can add a CTE to the new MERGE statement. After you define your WITH clause with the CTEs, you can then reference the CTEs as you would refer any other table.

What is the difference between Scope_identity () and Current_identity ()?

SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.

Is Upsert faster than merge?

merge is faster for merging. update is faster for updating.

What is the use of @@ identity and Scope_identity?

The @@identity function returns the last identity created in the same session. The scope_identity() function returns the last identity created in the same session and the same scope. The ident_current(name) returns the last identity created for a specific table or view in any session.


1 Answers

Use the inserted pseudo table in your OUTPUT clause:

DECLARE @Surrogate_KEY bigint


MERGE INTO [dbo].[MyTable] ChangeSet
USING (SELECT   @NaturalKey1 AS NaturalKey1, 
                @NaturalKey2 AS NaturalKey2, 
                @NaturalKey3 AS NaturalKey3,
                @Surrogate_KEY AS Surrogate_KEY) CurrentSet
ON  ChangeSet.NaturalKey1 = CurrentSet.NaturalKey1 AND 
    ChangeSet.NaturalKey2 = CurrentSet.NaturalKey2 AND 
    ChangeSet.NaturalKey3 = CurrentSet.NaturalKey3      
WHEN MATCHED THEN 
    UPDATE SET blah, blah, blah 

WHEN NOT MATCHED 
    THEN INSERT VALUES
       (
        blah, blah, blah
       )

output inserted.* ;

This returns whatever the values are in the table (for the affected rows) at the end of the statement.

like image 109
Damien_The_Unbeliever Avatar answered Oct 12 '22 00:10

Damien_The_Unbeliever