question

Varath avatar image
Varath asked

How to write merge statement inside another merge statement?

How to write merge statement inside another merge statement? please see the below logic. merge into table1 ---- when not matched merge into table2 --- when not matched insert into table1 matched update table2 matched update table1 Please help me to write the query. Thanks in advance. varath..
merge
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

GPO avatar image GPO commented ·
It might be just me but I don't really understand the question. What does your source data look like and how do you want it to look after processing?
1 Like 1 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
You have some good answers below. This web site runs by you voting and marking the best answers. For each helpful answer below, indicate this by clicking on the thumbs up symbol next to those answers. If any one answer lead to a solution, indicate this by clicking on the check mark next to that answer. This helps indicate your solution for anyone else with the same question.
0 Likes 0 ·
Varath avatar image
Varath answered
Hi, I have written code snippet for merge statement inside the condition of another merge statement. i am not able to write exact code here. i am facing syntax error near second merge. can you please provide some samples for two merge in one store procedure? code : Declare @variable declaration Merge Into Using (select query) On When not matched then Merge into Using (select query) On When not matched then Insert into records Matched Update records Matched Update records
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

GPO avatar image GPO commented ·
As I said, it would be a lot easier to help you if you posted some sample "before" data, and some sample "after" data so we know what you're trying to achieve.
1 Like 1 ·
ThomasRushton avatar image
ThomasRushton answered
I had a few thoughts about this. 1. a `MERGE` statement has only one target 2. You can't use the output from a `MERGE` statement as the input to a `MERGE` statement 3. And anyway, the output from a `MERGE` statement of the type you would generate wouldn't contain the rows you want to merge into your secondary table 4. You could capture the output from the MERGE / INSERT, and then run a second MERGE which only looks at items from your input table that don't appear in the previous merge output... 5. ...and now I've got a headache So, what you want might not be possible in the way that you want. I can see why you might want to do this, though.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.