Author |
Topic |
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-07-10 : 14:00:02
|
I have a simple merge statement that compares 2 tables and if the 2nd table does not have a matching entry I want the merge to insert the record.Here is the statement:MERGE [SALESLOGIX].[sysdba].[C_ACCTS_MARKETING_100] AS TargetUSING [SALESLOGIX].[sysdba].[ACCOUNT] AS SourceON Target.ACCOUNTID = Source.AccountID WHEN NOT MATCHED BY TARGET THEN INSERT (ACCOUNTID,CREATEDATE,INDUSTRYPRACTICE) VALUES (Source.Accountid,getdate(),'TEST');goThe error is:Msg 170, Level 15, State 1, Line 2Line 2: Incorrect syntax near '.'.Line 1: MERGE Line 2: [SALESLOGIX].[sysdba].[C_ACCTS_MARKETING_100] AS TargetAny idias, and thank you in advance for your help.Bryan Holmstrom |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-10 : 14:15:30
|
Target and Source are keywords that MERGE uses, so you should escape them like shown below:MERGE [SALESLOGIX].[sysdba].[C_ACCTS_MARKETING_100] AS [Target]USING [SALESLOGIX].[sysdba].[ACCOUNT] AS [Source]ON [Target].ACCOUNTID = [Source].AccountID WHEN NOT MATCHED BY Target THENINSERT (ACCOUNTID,CREATEDATE,INDUSTRYPRACTICE) VALUES ([Source].Accountid,getdate(),'TEST'); But a more preferable approach would be to avoid using reserved words as aliases like shown below:MERGE [SALESLOGIX].[sysdba].[C_ACCTS_MARKETING_100] AS TGTUSING [SALESLOGIX].[sysdba].[ACCOUNT] AS SRCON TGT.ACCOUNTID = SRC.AccountID WHEN NOT MATCHED BY TARGET THENINSERT (ACCOUNTID,CREATEDATE,INDUSTRYPRACTICE) VALUES (SRC.Accountid,getdate(),'TEST');go |
|
|
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-07-10 : 14:20:31
|
James thank you for the code refresher on reserved words. Little things escape the brain.I pasted your code above using the 2nd set and am still getting the error.Bryan Holmstrom |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2013-07-10 : 14:53:27
|
Which version of SQL you have? The code looks good to me at first look, but after james suggestion and your response of still getting the error, I performed following test and it workedcreate table #C_ACCTS_MARKETING_100 (ACCOUNTID int)create table #ACCOUNT (ACCOUNTID int)insert into #C_ACCTS_MARKETING_100 values (1)insert into #ACCOUNT values (2)MERGE #C_ACCTS_MARKETING_100 AS TargetUSING #ACCOUNT AS Source ON Target.ACCOUNTID = Source.AccountID WHEN NOT MATCHED BY TARGET THENINSERT (ACCOUNTID) VALUES (Source.Accountid);goselect * from #C_ACCTS_MARKETING_100CheersMIK |
|
|
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-07-10 : 15:15:10
|
I am using SQL SERVER 2008 R2Bryan Holmstrom |
|
|
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-07-10 : 15:18:18
|
SQL 2008 R2I just tried your code and got these errors:Msg 170, Level 15, State 1, Line 5Line 5: Incorrect syntax near 'MERGE'.Msg 208, Level 16, State 0, Line 1Invalid object name '#C_ACCTS_MARKETING_100'.Bryan Holmstrom |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-10 : 15:38:01
|
I think the server you are connected to is SQL 2005. You client tools may be SQL 2008, but the server itself is 2005. You can verify by running this query:SELECT @@version |
|
|
bholmstrom
Yak Posting Veteran
76 Posts |
Posted - 2013-07-10 : 15:55:46
|
Wow....SQL Server 2000Bryan Holmstrom |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-10 : 16:16:57
|
That would explain it :) With SQL 2000 you will have to use use a combination of updates, deletes and inserts. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-07-10 : 17:15:09
|
quote: Originally posted by James K Target and Source are keywords that MERGE uses, so you should escape them like shown below
FYI, That is not true. Source and Target are not reserved words. EDIT: Cut off part of the quote. |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-07-10 : 17:36:01
|
You are right. That was a guess based on the fact that merge syntax uses words like "WHEN NOT BY TARGET". |
|
|
|