Email query syntax error

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near '6'.

I have the following code which is returning the above error, i cant troubleshoot at all and wondering what is wrong with the query as it works fine without the email bit!

Thanks in advance
declare @myQuery varchar(8000), @eSubject varchar(254)
Set @myQuery='SELECT T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120) Date, sum(T0.[Quantity]) Total_fixes,
 sum(T0.LineTotal) Invoicing_total, sum(T3.[U_IIS_TIME]) TaskTime_Total, T0.[u_iis_INS2], T0.[u_IIS_INS3]
 FROM DLN1 T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
 INNER JOIN ODLN T2 ON T0.DocEntry = T2.DocEntry
 left outer join OSCN T3 on t3.ItemCode = t0.ItemCode and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)
 WHERE T2.[U_IIS_JTYPE] = '6' and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)
 and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))
 group by T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120), T0.[u_iis_INS2], T0.[u_IIS_INS3]'
 
Set @eSubject='REPORT: Completions per engineer'
 
            SET @myQuery = 'Select Body from TmpEmail'
            Create Table TmpEmail (Body varchar(254))
            Insert into TmpEmail Values('Hi All')
            Insert into TmpEmail Values('')
            Insert into TmpEmail Values('Please find attached the latest Invoice reports generated by the SQL Server.')
            Insert into TmpEmail Values('')
            Insert into TmpEmail Values('Thanks.')
            Insert into TmpEmail Values('')
            Insert into TmpEmail Values('MSSQL Database Server')
            exec master..xp_sendmail @recipients='mail.co.uk',
            @query=@myQuery, @no_header='TRUE',
            @copy_recipients='mySecondEmail?whoknows.com',
            @subject=@eSubject,
            @dbuse='advanced'
 
      Drop Table TmpEmail

Open in new window

KeirMcCannAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
declare @myQuery varchar(8000), @eSubject varchar(254)
Set @myQuery='SELECT T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120) Date, sum(T0.[Quantity]) Total_fixes,
 sum(T0.LineTotal) Invoicing_total, sum(T3.[U_IIS_TIME]) TaskTime_Total, T0.[u_iis_INS2], T0.[u_IIS_INS3]
 FROM DLN1 T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
 INNER JOIN ODLN T2 ON T0.DocEntry = T2.DocEntry
 left outer join OSCN T3 on t3.ItemCode = t0.ItemCode and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)
 WHERE T2.[U_IIS_JTYPE] = ''6'' and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)
 and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))
 group by T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120), T0.[u_iis_INS2], T0.[u_IIS_INS3]'
 
Set @eSubject='REPORT: Completions per engineer'
 
            SET @myQuery = 'Select Body from TmpEmail'
            Create Table TmpEmail (Body varchar(254))
            Insert into TmpEmail Values('Hi All')
            Insert into TmpEmail Values('')
            Insert into TmpEmail Values('Please find attached the latest Invoice reports generated by the SQL Server.')
            Insert into TmpEmail Values('')
            Insert into TmpEmail Values('Thanks.')
            Insert into TmpEmail Values('')
            Insert into TmpEmail Values('MSSQL Database Server')
            exec master..xp_sendmail @recipients='mail.co.uk',
            @query=@myQuery, @no_header='TRUE',
            @copy_recipients='mySecondEmail?whoknows.com',
            @subject=@eSubject,
            @dbuse='advanced'
 
      Drop Table TmpEmail

0
Patrick MatthewsCommented:
Normally the singlequote is a text qualifier, so to use it inside a string you have to use it twice:

 WHERE T2.[U_IIS_JTYPE] = ''6'' and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

Note that those are 2 instances each of singlequotes and NOT doublequotes.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KeirMcCannAuthor Commented:
thanks for the fast reply!

now i get:

Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@myQuery".
Msg 137, Level 15, State 1, Line 2
Must declare the scalar variable "@myQuery".
Msg 137, Level 15, State 1, Line 11
Must declare the scalar variable "@eSubject".
Msg 137, Level 15, State 1, Line 13
Must declare the scalar variable "@myQuery".
Msg 137, Level 15, State 2, Line 23
Must declare the scalar variable "@myQuery".


:(

poor me!
0
KeirMcCannAuthor Commented:
nm i did a typo!!!
0
KeirMcCannAuthor Commented:
thanks can someone provide me with a link to enable sql mail xp

its giving me this error, and i have no access to google etc :()

Msg 15281, Level 16, State 1, Procedure xp_sendmail, Line 1
SQL Server blocked access to procedure 'sys.xp_sendmail' of component 'SQL Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'SQL Mail XPs' by using sp_configure. For more information about enabling 'SQL Mail XPs', see "Surface Area Configuration" in SQL Server Books Online.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.