title: Merge Query In sql
body: <p>I am writing Merge Query in sql and if any changes in data based on id am updating the same and inserting new data if any.
  I am writing below query</p>
  <pre><code>    insert into
       DBNAME.TARGET_TABLE (COL1, ID, ID2)
      select
      COL1,
      ID,
      ID2
     from
     (
     MERGE INTO DBNAME.TARGET_TABLE AS TARGET USING (
    select
      COL1,
      ID,
      ID2
    from
      DBNAME.SOURCE_TABLE
  ) AS source (
  COL1, ID1, ID2
  ) ON (
  TARGET.ID = source.ID
  ) 
  WHEN MATCHED
  and (
    TARGET.ID2 &lt;&gt; source.ID2
    OR 
    TARGET.COL1 &lt;&gt; source.COL1
  ) 
  THEN
  UPDATE
  SET
    TARGET.COL1 = source.COL1,
    TARGET.ID2 = source.ID2 
    WHEN NOT MATCHED THEN 
    INSERT (COL1, ID, ID2)
  VALUES
    (
      source.COL1,
      source.ID,
      source.ID2
    )
    );
  </code></pre>
  <p>But I am getting below error message.</p>
  <pre><code>     You have an error in your SQL syntax; it seems the error is around: 'INTO DBNAME.TARGET_TABLE AS TARGET USING 
      ( select COL1, ID' at line 9
  </code></pre>
  <p>Can Someone Guide me what is the issue here?</p>