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 2012 Forums
 Transact-SQL (2012)
 How to Test this Simple MERGE Statement?

Author  Topic 

JacobPressures
Posting Yak Master

112 Posts

Posted - 2013-03-28 : 16:39:24


MERGE Batches b
USING (SELECT ID, FileName, CdbConnection, RdbConnection, CustomerID
FROM Batches
WHERE FileName = 'ex_20121009_unity-ny-a' AND CdbConnection = '\\mwfs1avm\MWPRIVATE\macdog\ex_20121009_unity-ny-a') o
ON o.ID = b.ID
WHEN MATCHED THEN SELECT 'Match Found'
WHEN NOT MATCHED THEN SELECT 'NO Match';

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-03-28 : 16:54:40
Run it?
Go to Top of Page

JacobPressures
Posting Yak Master

112 Posts

Posted - 2013-03-28 : 16:59:35
I get this error:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'SELECT'.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-03-28 : 17:18:51
Here are some links on how to post your question so that we can help you better:

http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

JacobPressures
Posting Yak Master

112 Posts

Posted - 2013-03-28 : 17:23:37
Actually the problem seems to be that you can't put a SELECT statement there thanks!

I'm trying to figure out how to TEST whether this is working or not.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-28 : 17:47:10
As you found out, MERGE is a DML statement - you can think of it as a combination of update, insert and delete. So the "WHEN" sections have to have one of those.

One way you can test is to write your actual query that has the insert/update/delete's in the WHEN sections, begin a transaction, run the merge statement, examine the results and then rollback.

If it is a hot database/table that you don't want to leave locked (when the transaction is left open), you can use output clause along with your insert/update/deletes to output the results and rollback immediately.
Go to Top of Page
   

- Advertisement -