SQL Server 2008 - Merge Script not working

1.2k views Asked by At

This merge script i'm writing isn't compiling and I believe I have the correct syntax.

MERGE into MyTable ct_current
USING (SELECT '0%' as Description, '0' as ShareAmount) ct_value
    ON ct_current.ShareAmount = ct_value.ShareAmount
WHEN MATCHED THEN 
    UPDATE SET ct_current.Description = '0%'
WHEN NOT MATCHED THEN
    INSERT (Description, ShareAmount)
    VALUES (ct_value.Description, ct_value.ShareAmount);
GO

Error:

Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'into'. Msg 102, Level 15, State 1, Line 2 Incorrect syntax near 'ct_value'.

2

There are 2 answers

0
contactmatt On BEST ANSWER

I realized that I had the SQL Server 2008 R2 client, but the server is SQL Server 2005.

0
scarpacci On

Try

MERGE MyTable AS ct_current
USING (SELECT '0%' as Description, '0' as ShareAmount) ct_value
  ON ct_current.ShareAmount = ct_value.ShareAmount
WHEN MATCHED THEN 
  UPDATE SET ct_current.Description = '0%'
WHEN NOT MATCHED THEN
  INSERT (Description, ShareAmount)
  VALUES (ct_value.Description, ct_value.ShareAmount);