?
Solved

Email query syntax error

Posted on 2008-11-07
5
Medium Priority
?
428 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:KeirMcCann
  • 3
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 22906951
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
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 22906963
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
 

Author Comment

by:KeirMcCann
ID: 22906990
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
 

Author Comment

by:KeirMcCann
ID: 22907000
nm i did a typo!!!
0
 

Author Comment

by:KeirMcCann
ID: 22907042
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question