简体   繁体   中英

SQL - Incorrect syntax near the keyword 'AND'

I'm trying to run an insert statement using three sets of data collected by using a union all.

If I run the select statements they work correctly, but if I run the insert statement I get the following error:

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'.**

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.

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!

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 < 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) < Getdate() 

Please see line 24 Cast(Datepart(yyyy, Getdate()) AS VARCHAR). Problems with brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM