Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 About Merge Statement

Author  Topic 

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-25 : 18:43:01
Hello,
I want to know what is the different between
Match By Target and match by Source
in MERGE
AND
Not Match by Target and Not Match by Source

lastly : when can I use them exactly?

also how can I insert into source table instead of target, and how can I delete from source table instead of target....!!
Thank you :)

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-04-25 : 18:56:47
target is the merge table, source is the using table.
So not matched by target is when there is a row in the using table that is not in the merge table - and you usually want to execute an insert.
Mathed by target and source - this is just matched - there is a row in the source and target and you usually check to see if there is a difference in the non-key columns and do an update.

You can't affect the using table (which doesn't even have to be a table - it's often a cte. You can output to a table variable and use that in a following statement though.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-25 : 19:17:43
thank you nigelrivett
Go to Top of Page

jooorj
Posting Yak Master

126 Posts

Posted - 2011-04-26 : 01:34:42
what is default ,do I must write (by Source) or (by Target)
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-04-26 : 04:23:07
It is fully documented in Books Online, see http://technet.microsoft.com/en-us/library/bb510625.aspx


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-04-26 : 04:26:46
quote:
Originally posted by jooorj

what is default ,do I must write (by Source) or (by Target)



Depends what you want to do. Two different options for two different scenarios. Read Nigel's post again

--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -