merge statement on an empty target table

I am doing a merge where the target table is empty.

I wonder in the inserted rows are  available during the merge, or are only visible to the merge statement after the merge?

I am getting no "match" when i think that the statement has just inserted a row which should be matched by the next source row.

Thanks
soozhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Please post the T-SQL MERGE statement into this question.
0
Olaf DoschkeSoftware DeveloperCommented:
You can see for yourself what happens during a merge, just look at the samples in the books online.

Declare @Target Table (id int, test char(40));
Declare @Source Table (id int, test char(40));
Declare @Out Table (id int, test char(40), actiontaken nvarchar(10), resultid int, resulttest char(40));

Insert Into @Target Values (1,'Hello');
Insert Into @Source Values (1,'Hello, World'),(2,'Bye'),(2,'Bye, World!');

MERGE @Target AS target
    USING @Source AS source
    ON (target.id = source.id)
WHEN MATCHED THEN 
    UPDATE SET test = source.test
WHEN NOT MATCHED THEN
    INSERT (id, test)
    VALUES (source.id, source.test)
    OUTPUT deleted.*, $action, inserted.* INTO @Out;

Select * From @Target;
Select * From @Out;

Open in new window


I modified your case in initially having a record in the target table to demonstrate the merge of it with the source. So finally your plan fails, a double id in source is not merged into one, it's inserted twice, as it originally didn't exist.

Bye, Olaf.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
soozhAuthor Commented:
Thanks i can see what is happening from your example.

I would really have liked the second row with id of 2 to update the target table rather than do a new insert.

SInce i am only working with 38 rows of data it may be easier to do the updates by hand!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Olaf DoschkeSoftware DeveloperCommented:
You'll only have inserts of the last record, don't you? So find each latest row and insert them or do a merge with each single record.

Bye, Olaf.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
soozh, do you still need help with this question?
0
Olaf DoschkeSoftware DeveloperCommented:
By now it should be done manually.
Anyway, a GROUP BY may also have solved this rather than a MERGE, or rather deleting the few double records you would overwrite with an update, if merge would work the way you expected.

Bye, Olaf.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.