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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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 Data DudeCommented:
Please post the T-SQL MERGE statement into this question.
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 ( =
    UPDATE SET test = source.test
    INSERT (id, test)
    VALUES (, 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.

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
soozhCEOAuthor 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!
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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.
Vitor MontalvãoMSSQL Senior EngineerCommented:
soozh, do you still need help with this question?
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.
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.