title: Incorrect syntax near ')' Error in the CTE
body: <p>I wrote the following CTE (Common Table Expression)</p>
  
  <pre><code>WITH PRODUCTION_CTE(ShortProdNo,BoatRefNumber,ProdNo, CustomerPoNumber,LoadDate, Trailer, VeadaBuilding)
  AS 
  (
      SELECT 
      FBS.BoatNumber AS ShortProdNo,
      UOD.BoatRefNumber AS BoatRefNumber,
      FBS.ProdNo AS ProdNo,
      UOD.CustomerPoNumber AS CustomerPoNumber,
      FBS.Shipped AS LoadDate, 
      FBS.TruckNum AS Trailer,
      (CASE 
      WHEN Rtrim(UOD.CustomerPoNumber)='VEADA-VS1' THEN  'Bldg10'
       ELSE 'Bldg4'
       END) AS VeadaBuilding
  FROM SysproCompanyV.dbo.FlatBenningtonShipping as FBS
  INNER JOIN SysproCompanyV.dbo.UsrOrderDetails as UOD
  ON FBS.BoatNumber=UOD.BoatRefNumber)
  </code></pre>
  
  <p>I am getting the following Error from the above CTE:</p>
  
  <pre><code>Msg 102, Level 15, State 1, Line 17
  Incorrect syntax near ')'.
  </code></pre>
  
  <p>I am not sure why it is happening as Inner Joins are allowed in the CTE, all the parenthesis are closed and the names are correctly declared. </p>