0

I have got a T-SQL MERGE command that works fine on my SQL Server 2008. Unfortunately, the code has to work on a SQL Server 2005 database, too.

What would I have to change, to make the following code runnable in SQL Server 2005?

Thank you for your help.

DECLARE @CNVAL varchar(100) = 'xyz' DECLARE @ATRBT varchar(100) = 'abcde' DECLARE @CON varchar(100) = 'fgh' 
MERGE into CNEXTR as target
USING (VALUES( @CON, @ATRBT, @CNVAL))
     AS source([Name], ATTR, CNVAL)
     ON (target.[Name] = source.[Name])
     AND (target.ATTR = source.ATTR)
WHEN MATCHED THEN 
     UPDATE SET 
         CNVAL = source.CNVAL 
WHEN NOT MATCHED THEN 
     INSERT([Name], ATTR, CNVAL) 
     VALUES(source.[Name], source.ATTR, source.CNVAL);

1 Answer 1

5

Change this to

BEGIN TRAN

   UPDATE CNEXTR
   SET CNVAL = @CNVAL
   WHERE [Name] = @CON AND ATTR = @ATRBT;

   IF @@ROWCOUNT = 0
       INSERT CNEXTR ([Name], ATTR, CNVAL)
       VALUES(@CON, @ATRBT, @CNVAL);

COMMIT TRAN

Each statement will be far simpler than the MERGE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.