Merge Statement

Is there a merge statement in SQL Server 2000

I want to combine update and insert statement into single statement as follows.

MERGE INTO MyTable
USING MyTempTable
ON MyTempTable.MatchingField1 = MyTable.MatchingField1
WHEN MATCHED THEN
UPDATE UpdateField1 = MyTempTable.UpdateField1
WHEN NOT MATCHED THEN
INSERT VALUES(MyTempTable.MatchingField1, MyTempTable.UpdateField1)

Currently if I try to run this stmt, it gives error "Incorrect syntax near the keyword 'INTO'."

Thanks




Answer this question

Merge Statement

  • Der Sven

    We are considering adding MERGE feature in a future version of SQL Server. It has been requested frequently. For now you have to use a technique like Adam outlined.

  • chaza

    No, SQL Server has no such statement.  The closest pattern I've found is:
     
    UPDATE Tbl
    SET
        Col1 = @Param1,
        Col2 = @Param2,
        ...
    WHERE YourPK = @PKParam
     
    IF @@ROWCOUNT = 0
    BEGIN
      INSERT Tbl
      (
        Col1,
        Col2,
        ...,
        YourPK
      )
      VALUES
      (
        @Param1,
        @Param2,
        ...,
        @YourPK
      )
    END
     
     

    --
    Adam Machanic
    Pro SQL Server 2005, available now
    http://www..apress.com/book/bookDisplay.html bID=457
    --
     
     

    Is there a merge statement in SQL Server 2000

    I want to combine update and insert statement into single statement as follows.

    MERGE INTO MyTable
    USING MyTempTable
    ON MyTempTable.MatchingField1 = MyTable.MatchingField1
    WHEN MATCHED THEN
    UPDATE UpdateField1 = MyTempTable.UpdateField1
    WHEN NOT MATCHED THEN
    INSERT VALUES(MyTempTable.MatchingField1, MyTempTable.UpdateField1)

    Currently if I try to run this stmt, it gives error "Incorrect syntax near the keyword 'INTO'."

    Thanks


  • Merge Statement