0

I tried several times to execute the following MERGE statement, but I always got the same error message:

Incorrect syntax near the keyword 'AS'.

Any idea why this error?

    CREATE TABLE StudentDetails
    (
        StudentID INTEGER PRIMARY KEY,
        StudentName VARCHAR(15)
    )
    GO

    INSERT INTO StudentDetails
    VALUES(1,'SMITH')
    INSERT INTO StudentDetails
    VALUES(2,'ALLEN')
    INSERT INTO StudentDetails
    VALUES(3,'JONES')
    INSERT INTO StudentDetails
    VALUES(4,'MARTIN')
    INSERT INTO StudentDetails
    VALUES(5,'JAMES')
    GO

    CREATE TABLE StudentTotalMarks
    (
        StudentID INTEGER REFERENCES StudentDetails,
        StudentMarks INTEGER
    )
    GO

    INSERT INTO StudentTotalMarks
    VALUES(1,230)
    INSERT INTO StudentTotalMarks
    VALUES(2,255)
    INSERT INTO StudentTotalMarks
    VALUES(3,200)
    GO

    MERGE StudentTotalMarks AS stm
    USING (SELECT StudentID,StudentName FROM StudentDetails) AS sd
    ON stm.StudentID = sd.StudentID
    WHEN MATCHED AND stm.StudentMarks > 250 THEN DELETE
    WHEN MATCHED THEN UPDATE SET stm.StudentMarks = stm.StudentMarks + 25
    WHEN NOT MATCHED THEN
    INSERT(StudentID,StudentMarks)
    VALUES(sd.StudentID,25);
2
  • In future, please format your code, so we can read it. Jul 12, 2011 at 12:12
  • 1
    The code provided works fine, no syntax errors.
    – cairnz
    Jul 12, 2011 at 12:28

2 Answers 2

3

Your code works exactly as written for me.

Are you sure you are using SQL Server 2008?

2
  • I don't understand what happens in my case, I do use SQL Server 2008 R2
    – largo68
    Jul 12, 2011 at 12:26
  • @largo - Is your Management Studio 2008 AND your server/database? It's possible to connect to an older database or older server from a newer SSMS version.
    – JNK
    Jul 12, 2011 at 12:39
1

You need SQL Server 2008 to use merge.

I have modified your code to use table variables instead just to prove that it works in SQL Server 2008.

You can test it here: https://data.stackexchange.com/stackoverflow/q/105423/test-merge

6
  • 1
    @JNK - yes it does but the error message is from SQL Server 2005 not recognizing merge. His code is correct as it is. Jul 12, 2011 at 12:21
  • I got that after I posted the comment when the code worked for me :)
    – JNK
    Jul 12, 2011 at 12:22
  • @JNK - BTW, this merge statement used here is ignorant to compatibility level settings. Jul 12, 2011 at 12:23
  • 3
    @largo68 - Are you sure your DB is 2008 not only SQL Server Managment Studio 2008? What does this tell you? select @@version Jul 12, 2011 at 12:26
  • Microsoft SQL Server 2000 - 8.00.2282 (Intel X86) Dec 30 2008 02:22:41 Copyright (c) 1988-2003 Microsoft Corporation Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
    – largo68
    Jul 12, 2011 at 12:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.