Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Statement and Identity Insert

I am using Merge statement to Insert/Update data in my stored procedure.

MERGE [dbo].[tReserveData_4541] AS Target
USING (SELECT * FROM  [dbo].[tblcangrowhitesh]) AS Source
ON ( [Source].[PK_ID] =  [Target].[PK_ID])

WHEN MATCHED THEN
    UPDATE SET [Target].[mgender] = Source.[mgender]

WHEN NOT MATCHED BY TARGET THEN
    INSERT ([Granularity], [PK_ID], [ROWID], 
            [mgender], [mma1], [mma2], [mma3], 
            [mmadmincost], [mmcumulativevolume], 
            [mmcurrency], [mmdate],
            [mmfileimporteddate], [mmfilename])
    VALUES ([Source].[Granularity], [Source].[PK_ID], [Source].[ROWID],
            [Source].[mgender], [Source].[mma1], [Source].[mma2], [Source].[mma3], 
            [Source].[mmadmincost], [Source].[mmcumulativevolume],
            [Source].[mmcurrency], [Source].[mmdate],
            [Source].[mmfileimporteddate], [Source].[mmfilename])

As you can see I am going to insert identity column i.e. [PK_ID] in the MERGE statement. But I am unable to do so.

like image 938
hitesh agja Avatar asked Mar 09 '26 18:03

hitesh agja


1 Answers

I'm not that familiar with the MERGE command, but as you're inserting directly into an identity column, would you not have to use the IDENTITY_INSERT command? e.g.

SET IDENTITY_INSERT [dbo].[tReserveData_4541] on

MERGE...

SET IDENTITY_INSERT [dbo].[tReserveData_4541] off
like image 79
Philip Kelley Avatar answered Mar 11 '26 14:03

Philip Kelley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!