title: Merge statement error in SQL Server 2008 (Incorrect syntax near)
body: <p>I'm trying to use "MERGE" in two temporary tables, I failed to get results.</p>
  
  <p>Error: Incorrect syntax near the keyword 'AS'.</p>
  
  <pre><code>    Create Table #tmp1
      (
      [Server] varchar(4),
      [DateTime] datetime,
      IdComponent int,
      AvgTimeTaken int
      )
  
      Create Table #tmp2
      (
      [Server] varchar(4),
      [DateTime] datetime,
      IdComponent int,
      AvgTimeTaken int
      )
  
      insert into #tmp1 values ('BE01','2012-08-01 00:00:00',1,100)
      insert into #tmp1 values ('BE02','2012-08-01 00:00:00',2,100)
      insert into #tmp1 values ('BE03','2012-08-01 00:00:00',3,100)
      insert into #tmp1 values ('BE04','2012-08-01 00:00:00',4,100)
      insert into #tmp1 values ('BE05','2012-08-01 00:00:00',5,100)
  
  
      insert into #tmp2 values ('BE01','2012-08-01 00:00:00',1,100)
      insert into #tmp2 values ('BE02','2012-08-01 00:00:00',2,200)
      insert into #tmp2 values ('BE03','2012-08-01 00:00:00',3,300)
      insert into #tmp2 values ('BE04','2012-08-01 01:00:00',4,400)
      insert into #tmp2 values ('BE05','2012-08-01 02:00:00',5,500)
  
      MERGE #tmp1 AS [Target]
      USING #tmp2 AS [Source]
      ON ([Target].[Server] = [Source].[Server] 
          AND [Target].[DateTime] =  [Source].[DateTime] 
          AND [Target].[IdComponent] = [Source].[IdComponent])
      WHEN MATCHED THEN 
          UPDATE
          SET [Target].AvgTimeTaken = [Source].AvgTimeTaken
      WHEN NOT MATCHED THEN   
          INSERT ([Target].[Server], [Target].[DateTime], [Target].IdComponent, [Target].AvgTimeTaken)
  VALUES ([Source].[Server], [Source].[DateTime], [Source].IdComponent, [Source].AvgTimeTaken);
  </code></pre>
  
  <p>I'm not sure I can be wrong. At the end also has a semicolon.
  Help please!</p>