title: Merge statement in Sql server with target table as existing table in Sql Server 2008
body: <p>QUESTION:I have table ARMS.RefRollno in my database and with roll no and rank now i have query which is returning a dataset of roll and rank .If my table contains that row then i need to update it and if not that i want to insert a new row in respect to that roll no.</p>
  
  <pre><code>    create table #temp
      (
        ROLLNO varchar(100),
        Ranking varchar(100),
        TestRecID varchar(100)
      )
      INSERT INTO #temp (ROLLNO, Ranking,TestRecID) EXEC [ARMS].[GetStudentResultForUpdateRank] '412'
  
     MERGE ARMS.RefRollno AS C
      USING #temp AS CT
      ON C.TestRecID = CT.TestRecID
      WHEN MATCHED THEN
      UPDATE SET
      C.RefRank = CT.Ranking      
      WHEN NOT MATCHED THEN 
      INSERT (TestRecId,RefRollNo, RefRank,IsActive,CreatedDate)
      VALUES (CT.TestRecID,CT.ROLLNO,CT.Ranking, 1,getdate());
      drop table #temp
  </code></pre>
  
  <blockquote>
    <p>Here ARMS.RefRollno is my existing table in Database.
    Any Help is appreciated.</p>
  </blockquote>
  
  <pre><code>    ** Error by Sql Server:Incorrect syntax near 'MERGE'.**
  </code></pre>