Jacob asked the perfect question: give me the MERGE syntax .
Each answer there immediately goes into the most difficult case that they may think of; hiding syntax with extraneous confusion.
Mark answered :
MERGE member_topic AS target USING someOtherTable AS source ON target.mt_member = source.mt_member AND source.mt_member = 0 AND source.mt_topic = 110 WHEN MATCHED THEN UPDATE SET mt_notes = 'test' WHEN NOT MATCHED THEN INSERT (mt_member, mt_topic, mt_notes) VALUES (0, 110, 'test') ;
Looking at this answer, I'm as confused as Jacob is:
I don't have someOtherTable
Mark suggested that someOtherTable is a dummy placeholder value - it doesn't matter that you don't have this table.
I am trying to do this and SQL Server is complaining
Invalid object name 'someOtherTable'.
This leaves me trying to figure out what USING in USING foo means if it doesn't matter (other than the really important ones).
What is USING used with foo when using the MERGE SQL Server 2008 syntax?
Bonus Question
What is UPSERT syntax with MERGE:
IF (rowExists) UPDATE Users SET Firstname='Ian', LastName='Boyd' WHERE Username='iboyd' ELSE INSERT INTO Users (UserGUID, Username, FirstName, LastName, AuthenticationMethod) VALUES ('{77410DC5-7A3E-4F1A-82C6-8EFB3068DE66}', 'iboyd', 'Ian', 'Boyd', 'Windows')
becomes (the exact code I tried):
begin transaction MERGE Users USING foo ON Users.UserName = foo.UserName WHEN MATCHED THEN UPDATE SET Firstname = foo.FirstName, Lastname = foo.LastName WHEN NOT MATCHED THEN INSERT (UserGUID, Username, FirstName, LastName, AuthenticationMethod) VALUES ('{77410DC5-7A3E-4F1A-82C6-8EFB3068DE66}', 'iboyd', 'Ian', 'Boyd', 'Windows') ;
?
With a Users table containing columns:
UserGUID uniqueidentifier Username varchar(50) FirstName varchar(50) LastName varchar(50) AuthenticationMethod varchar(50)
Update:
USING <table_source>
Where table_source :
table_or_view_name [ [ AS ] table_alias ] [ <tablesample_clause> ] [ WITH ( table_hint [ [ , ]...n ] ) ] | rowset_function [ [ AS ] table_alias ] [ ( bulk_column_alias [ ,...n ] ) ] | user_defined_function [ [ AS ] table_alias ] | OPENXML <openxml_clause> | derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ] | <joined_table> | <pivoted_table> | <unpivoted_table>
Where joined_table :
undefined
Where pivoted_table :
undefined
Where unpivoted_table :
undefined