title: SQL Server 2008: Incorrect syntax near the keyword 'AS'
body: <p>I have a problem with a program I inherited. I needed to adjust a query but nog the query doesn't work anymore.</p>
  
  <p>This is the query I got (don't judge me, it isn't my query :) )</p>
  
  <pre><code>SELECT     
     SUM(DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) + 1) AS AantalDagen,    
     verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeSectie,
     (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
      FROM PEBSectie
      WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = '00')) AS BSNS,
     (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
      FROM PEBSectie AS PEBSectie_1
      WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = '00')) AS AFD,
     (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
      FROM PEBSectie AS PEBSectie_1
      WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND (CodeSectie = '00')) AS GRP,
     (SELECT TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
      FROM PEBSectie AS PEBSectie_1
      WHERE (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND (CodeSectie = verz1.CodeSectie)) AS SEC
  FROM         
      Verzorging AS verz1 
  LEFT OUTER JOIN
      AangepastWerk AS aw1 ON verz1.VerzorgingId = aw1.VerzorgingId
  WHERE     
      (verz1.EindDatumAW &lt;= @EndDate) AND 
      (verz1.BeginDatumAW &gt;= @StartDate) AND 
      (aw1.Verwijderd = 0) AND               &lt;------------ First statement added
      (aw1.NietErkend = 0 OR aw1.NietErkend IS NULL) AND 
      (DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) &gt; 0))
  GROUP BY 
       verz1.CodeBusiness, verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeGroep, 
       verz1.CodeSectie, verz1.TypeAWOfWO, verz1.TypeOngeval
  HAVING      
       (verz1.TypeAWOfWO = 'A') AND (verz1.TypeOngeval = 'AO') OR
       (verz1.TypeAWOfWO = 'Arbeidsongeval') OR
       (verz1.TypeAWOfWO = 'arbeidsongeval')
  UNION ALL
  SELECT     SUM(DATEDIFF(day, @StartDate, verz1.EindDatumAW) + 1) AS AantalDagen, verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeSectie,
                        (SELECT     TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
                          FROM          PEBSectie AS PEBSectie_3
                          WHERE      (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = '00')) AS BSNS,
                        (SELECT     TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
                          FROM          PEBSectie AS PEBSectie_1
                          WHERE      (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = '00')) AS AFD,
                        (SELECT     TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
                          FROM          PEBSectie AS PEBSectie_1
                          WHERE      (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND 
                                                 (CodeSectie = '00')) AS GRP,
                        (SELECT     TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
                          FROM          PEBSectie AS PEBSectie_1
                          WHERE      (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = verz1.CodeAfdeling) AND (CodeGroep = verz1.CodeGroep) AND 
                                                 (CodeSectie = verz1.CodeSectie)) AS SEC
  FROM         Verzorging AS verz1 LEFT OUTER JOIN
                    AangepastWerk AS aw1 ON verz1.VerzorgingId = aw1.VerzorgingId
  WHERE     (verz1.EindDatumAW &lt;= @EndDate) 
  AND (verz1.EindDatumAW &gt;= @StartDate) 
  AND (verz1.BeginDatumAW &lt; @StartDate) 
  AND (aw1.Verwijderd = 0)                   &lt;------------ Second statement added
  AND (aw1.NietErkend = 0 OR aw1.NietErkend IS NULL) 
  AND (DATEDIFF(day, verz1.BeginDatumAW, verz1.EindDatumAW) &gt; 0)
  GROUP BY verz1.CodeBusiness, verz1.CodeBedrijf, verz1.CodeAfdeling, verz1.CodeGroep, verz1.CodeSectie, verz1.TypeAWOfWO, verz1.TypeOngeval
  HAVING      (verz1.TypeAWOfWO = 'A') AND (verz1.TypeOngeval = 'AO') OR
                    (verz1.TypeAWOfWO = 'Arbeidsongeval') OR
                    (verz1.TypeAWOfWO = 'arbeidsongeval')
  </code></pre>
  
  <p><strong>Changes:</strong> In both where statements I added the following line:</p>
  
  <pre><code> AND (aw1.Verwijderd = 0)
  </code></pre>
  
  <p>After i added this i keep getting the problems with the following subqueries (for each of the subqueries i get the same error).</p>
  
  <pre><code>(SELECT     TOP (1) RTRIM(OmschrijvingSectie) AS Expr1
                          FROM          PEBSectie AS PEBSectie_3
                          WHERE      (CodeBusiness = verz1.CodeBusiness) AND (CodeAfdeling = '00')) AS BSNS,
  </code></pre>
  
  <p>The error I get is:</p>
  
  <blockquote>
    <p>Msg 156, Level 15, State 1, Line 28<br>
    Incorrect syntax near the keyword 'AS'.</p>
  </blockquote>
  
  <p>I can't seem to figure out what I've done/ what has gone wrong. I hoped a pair of fresh eyes might see something I've missed.</p>
  
  <p><strong>Note</strong>: The error only occurs after the <strong><em>UNION ALL</em></strong></p>
  
  <p>I thank you for all your patience</p>
  
  <p>with kind regards</p>
  
  <p><strong>Edit:</strong> if possible: major Modifications to the query are to be avoided. </p>