title: SQL - Incorrect syntax near the keyword 'AND'
body: <p>I'm trying to run an insert statement using three sets of data collected by using a union all.</p>
  
  <p>If I run the select statements they work correctly, but if I run the insert statement I get the following error:</p>
  
  <pre><code>Msg 137, Level 15, State 2, Line 6
  
  Must declare the scalar variable "@_EventSessionID".
  
  Msg 137, Level 15, State 2, Line 23
  
  Must declare the scalar variable "@_EventSessionID".
  
  Msg 137, Level 15, State 2, Line 40
  
  Must declare the scalar variable "@_EventSessionID".
  
  **Msg 156, Level 15, State 1, Line 52
  Incorrect syntax near the keyword 'AND'.**
  </code></pre>
  
  <p>Declaring the scalar variable is a logical error, but these will be supplied by the application I run this from. The thing I can't figure out is the message depicted in bold. </p>
  
  <p>The last part of the query (including the insert into part) I'm trying to run is the following. Hope anybody can help me out! </p>
  
  <pre><code>INSERT INTO _conn_tb_request 
              (_mode, 
               type, 
               _eventsessionid, 
               _task, 
               employeeid, 
               freeintfield_01, 
               freetextfield_01, 
               description, 
               startdate, 
               enddate, 
               freeguidfield_01, 
               freeguidfield_02, 
               freetextfield_03) 
  SELECT 0, 
         96, 
         @_EventSessionID, 
         @_EventDescription, 
         res_id, 
         fullname, 
         Datepart(yyyy, Getdate()), 
         'RGA', 
         'RGA gesprek ' 
         + Cast(Datepart(yyyy, Getdate()) AS VARCHAR) 
         + ' - ' + fullname, 
         Dateadd(yy, Datediff(yy, 0, Getdate()), 0), 
         Dateadd(mm, 2, Dateadd(yy, Datediff(yy, 0, Getdate()), 0)), 
         '3C11435F-598B-4164-8CFD-9A943ED30262', 
         ab.id, 
         ab.freetextfield_02 
  FROM   humres hu (nolock) 
         LEFT JOIN absences ab (nolock) 
                ON hu.res_id = ab.empid 
                   AND ab.type = 96 
                   AND ab.freetextfield_01 = 'RGA' 
                   AND ab.freeintfield_01 = Datepart(yyyy, Dateadd(yy, -1, ( 
                                                           Getdate() )) 
                                            ) 
  WHERE  ldatindienst &lt; Dateadd(yy, -1, ( Dateadd(dd, 1, ( Dateadd(qq, 
                                                           Datediff(qq, 0, 
                                                           Getdate()), 
                                                                  -1) ) 
                                                                ) )) 
         AND emp_type IN ( 'E' ) 
         AND emp_stat = 'A' 
         AND hu.res_id NOT IN (SELECT empid 
                               FROM   absences (nolock) 
                               WHERE  type = 96 
                                      AND freetextfield_01 = 'RGA' 
                                      AND freeintfield_01 = Datepart(yyyy, Getdate 
                                                            ())) 
         AND Dateadd(yy, Datediff(yy, 0, Getdate()), 0) &lt; Getdate() 
  </code></pre>