title: Invalid Object name 'Source' while using merge
body: <p>I am using this in a merge statement but now I am getting the error Invalid object name 'Source'.</p>
  
  <p>I am not getting this error on any of my other merge statements. Source is below. I have anonymized to a certain extent so please let me know if it isn't clear</p>
  
  <pre><code> MERGE dbo.Destination WITH (HOLDLOCK) AS Target
  USING
  (
  SELECT DISTINCT id, col3,   
   CAST(LEFT(DATEADD(m,months,CONVERT(date,CONCAT(origination,'/01'))),7) as   varchar(20))  as CalendarMonth,
   col1, col2,col4 FROM dbo.Staging
  ) AS Source
  ON (Target.ID=Source.id
  AND Target.Month=Source.col3)
      WHEN MATCHED THEN
          UPDATE SET
          Target.CalendarMonth=Source.CalendarMonth,
          Target.colF= (SELECT CASE WHEN col1&gt;0 THEN 1 END AS colF FROM Source),
          Target.colD=(SELECT CASE WHEN col4&gt;0 THEN 1 END AS colD
                                      FROM Source),
          Target.colC=(SELECT CASE WHEN col1=0 AND col2=0
                                      THEN 1 END AS colC FROM Source),
          Target.colB(SELECT CASE WHEN col1&gt;0 AND col2
                                      THEN 1 END AS colB FROM Source),
          Target.colG=(SELECT CASE WHEN col1&gt;0 THEN col1 END AS colG FROM Source),
          Target.colE=(SELECT CASE WHEN col4&gt;0 THEN col3 END
                                      AS colE FROM Source),
          Target.PaidMonth=(SELECT CASE WHEN col1=0 AND col2=0
                                      THEN col3 END AS PaidMonth 
                                      FROM Source),
          WHEN NOT MATCHED BY TARGET THEN
          INSERT
          (Destination Table columns
          )
          VALUES
          (   Source values including derived values from above);
  
  END
  </code></pre>
  
  <p>Thanks in advance for your help!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>@Sam cd's recommendation helped where i replaced the select statement in the derived columns with CASE WHEN col4>0 THEN col3 END and so on. Thanks everyone for your help!</p>