| Author |
Topic |
|
willware
Starting Member
6 Posts |
Posted - 2011-08-22 : 22:43:59
|
| i am trying to insert or update a table from another table here is my script with the error.USE DEMS;GOBEGIN TRAN;MERGE DEMS_acct AS TUSING DemAcct AS SON (T.acct_ID = S.ACCT1 AND T.acctsetcode = S.AcSetId1) WHEN NOT MATCHED BY DEMS_acct THEN INSERT(site_ID, acct_ID, subid, subsubid, acctsetcode) VALUES(S.Sitid1, S.ACCT1, S.SubID1, S.SuSuID1, S.AcSetId1)WHEN MATCHED THEN UPDATE SET T.site_ID = S.Sitid1, T.acct_ID = S.ACCT1, T.subid = S.SubID1, T.subsubid = S.SuSuID1, T.acctsetcode = S.AcSetId1Msg 102, Level 15, State 1, Line 2Incorrect syntax near 'MERGE'.William E Ware |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2011-08-22 : 22:47:41
|
| Which version of sql server you are using?Harsh Athalyehttp://www.letsgeek.net/ |
 |
|
|
willware
Starting Member
6 Posts |
Posted - 2011-08-22 : 22:54:47
|
| 2008William E Ware |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2011-08-22 : 23:54:57
|
How about this?USE DEMSGOBEGIN TRANMERGE DEMS_acct AS TUSING DemAcct AS SON (T.acct_ID = S.ACCT1 AND T.acctsetcode = S.AcSetId1)WHEN MATCHEDTHEN UPDATE SET T.site_ID = S.Sitid1, T.acct_ID = S.ACCT1, T.subid = S.SubID1, T.subsubid = S.SuSuID1, T.acctsetcode = S.AcSetId1WHEN NOT MATCHED BY TARGETTHEN INSERT(site_ID, acct_ID, subid, subsubid, acctsetcode) VALUES(S.Sitid1, S.ACCT1, S.SubID1, S.SuSuID1, S.AcSetId1) Harsh Athalyehttp://www.letsgeek.net/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 00:16:31
|
| and MERGE should terminate with a ; character------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
willware
Starting Member
6 Posts |
Posted - 2011-08-23 : 07:01:09
|
| USE DEMSGOBEGIN TRAN;MERGE DEMS_acct AS TUSING DemAcct AS SON (T.acct_ID = S.ACCT1 AND T.acctsetcode = S.AcSetId1)WHEN MATCHED THEN UPDATE SET T.site_ID = S.Sitid1, T.acct_ID = S.ACCT1, T.subid = S.SubID1, T.subsubid = S.SuSuID1, T.acctsetcode = S.AcSetId1WHEN NOT MATCHED BY TARGET THEN INSERT(site_ID, acct_ID, subid, subsubid, acctsetcode) VALUES(S.Sitid1, S.ACCT1, S.SubID1, S.SuSuID1, S.AcSetId1);Msg 102, Level 15, State 1, Line 2Incorrect syntax near 'MERGE'.William E Ware |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 07:17:48
|
| are you using sql 2008? what does below return?SELECT @@VERSIONGOEXEC sp_dbcmptlevel 'your db name'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
willware
Starting Member
6 Posts |
Posted - 2011-08-23 : 07:38:56
|
| Sorry it is 2005Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) Nov 24 2008 13:01:59 Copyright (c) 1988-2005 Microsoft Corporation Developer Edition on Windows NT 6.0 (Build 6001: Service Pack 1)William E Ware |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 07:43:32
|
MERGE is available only from SQL 2008 onwardsif its sql 2005 you should use UPDATE/INSERT combination to implement aboveit should be like INSERT DEMS_acct (site_ID, acct_ID, subid, subsubid, acctsetcode) SELECT S.Sitid1, S.ACCT1, S.SubID1, S.SuSuID1, S.AcSetId1FROM DemAcct S LEFT JOIN DEMS_acct TON T.acct_ID = S.ACCT1 AND T.acctsetcode = S.AcSetId1WHERE T.acct_ID IS NULLUPDATE TSET T.site_ID = S.Sitid1, T.acct_ID = S.ACCT1, T.subid = S.SubID1, T.subsubid = S.SuSuID1, T.acctsetcode = S.AcSetId1FROM DemAcct S INNER JOIN DEMS_acct TON T.acct_ID = S.ACCT1 AND T.acctsetcode = S.AcSetId1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
willware
Starting Member
6 Posts |
Posted - 2011-08-23 : 08:27:58
|
| Thank youWilliam E Ware |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 09:22:20
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|