dbTalk Databases Forums  

Trying to perform a query using XP Sendmail

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Trying to perform a query using XP Sendmail in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mike K
 
Posts: n/a

Default Trying to perform a query using XP Sendmail - 12-13-2007 , 11:40 AM






Hi, I am new to SQL and I am trying to email my group results of a
query as part of a message body. I created a SP and I am using
XPSendmail. Everything works fine except for the fact that the body of
the email doesn't have my query results. I am hoping somebody can
check what I have here and maybe give me some insights. I really
appreciate all your help. Thanks! Here is the sp:

CREATE PROCEDURE [dbo].[spEmailVariance]
(
@SubjectLine as varchar(500),
@EmailRecipient VARCHAR(100)
)
AS

DECLARE @strBody varchar(1000)
set @SubjectLine = 'ZZZZZZZ'
SET @strBody =
'SELECT FSA_Weekly_Flash.dbo.Current_Period_Week_Flash.Fis cal_Year AS
Accounting_Year,

FSA_Weekly_Flash.dbo.Current_Period_Week_Flash.WK_ IN_FYEAR,
FSA_Weekly_Flash.dbo.Branch.Description AS Location,
FSA_Weekly_Flash.dbo.GL_Account.GL_Account,
SUM(FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Ba lance_Flash.Amount)
AS Flash,

SUM(FSA_Findata.dbo.Weekly_Account_Balance.Amount) AS FinData,
SUM(FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Ba lance_Flash.Amount)
-
SUM(FSA_Findata.dbo.Weekly_Account_Balance.Amount) AS Difference
FROM FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash
INNER JOIN
FSA_Weekly_Flash.dbo.Current_Period_Week_Flash
ON

FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash.Accounting_Year
= FSA_Weekly_Flash.dbo.Current_Period_Week_Flash.Fis cal_Year
AND

FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash.Week_Number
= FSA_Weekly_Flash.dbo.Current_Period_Week_Flash.WK_ IN_FYEAR
INNER JOIN
FSA_Weekly_Flash.dbo.Branch ON

FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash.Branch =
FSA_Weekly_Flash.dbo.Branch.Branch_Number INNER JOIN
dbo.GL_Account ON
SUBSTRING(FSA_Weekly_Flash.dbo.Staging_Weekly_Acco unt_Balance_Flash.GL_Account,
1, 7)
= dbo.GL_Account.GL_Account INNER JOIN
FSA_Findata.dbo.Weekly_Account_Balance ON

FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash.GL_Account =
FSA_Findata.dbo.Weekly_Account_Balance.GL_Account AND

FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash.Accounting_Year
= FSA_Findata.dbo.Weekly_Account_Balance.Accounting_ Year AND

FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash.Week_Number
= FSA_Findata.dbo.Weekly_Account_Balance.Week_Number AND

FSA_Weekly_Flash.dbo.Staging_Weekly_Account_Balanc e_Flash.Branch =
FSA_Findata.dbo.Weekly_Account_Balance.Branch
GROUP BY FSA_Weekly_Flash.dbo.Current_Period_Week_Flash.Fis cal_Year,
FSA_Weekly_Flash.dbo.Branch.Description, dbo.GL_Account.GL_Account,

FSA_Weekly_Flash.dbo.Current_Period_Week_Flash.WK_ IN_FYEAR
HAVING (dbo.GL_Account.GL_Account = "500-001")
ORDER BY FSA_Weekly_Flash.dbo.Branch.Description'


exec master.dbo.xp_sendmail
@recipients= 'xxxx (AT) xxxxx (DOT) com',
@subject= @SubjectLine


RETURN


GO

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-13-2007 , 01:40 PM






Hi Mike,

Seems you are missing the @query parameter of xp_sendmail. Try changing you
code to:

exec master.dbo.xp_sendmail
@recipients = 'xxxx (AT) xxxxx (DOT) com',
@subject = @SubjectLine,
@query = @strBody

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #3  
Old   
Mike K
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-13-2007 , 02:16 PM



On Dec 13, 12:40 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
Hi Mike,

Seems you are missing the @query parameter of xp_sendmail. Try changing you
code to:

exec master.dbo.xp_sendmail
@recipients = 'x... (AT) xxxxx (DOT) com',
@subject = @SubjectLine,
@query = @strBody

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
Thank you! I added that but now i am getting the following error:
"ODBC error 170 (42000) Line 10: Incorrect syntax near
'FSA_Weekly_Fla'."

This query runs fine when i run it by itself i.e without embedding it
part of this sp. I am going to troubleshoot and see what the syntax
error is.


Reply With Quote
  #4  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-13-2007 , 02:50 PM



You declared @strBody as varchar(1000) and in fact your SQL statement is
around 2339 characters (according to a quick copy/paste to a text editor).
Try declaring as varchar(3000).

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #5  
Old   
Mike K
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-13-2007 , 03:48 PM



On Dec 13, 1:50 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
You declared @strBody as varchar(1000) and in fact your SQL statement is
around 2339 characters (according to a quick copy/paste to a text editor).
Try declaring as varchar(3000).

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
Thanks that really helped. I glanced over it and yet totally missed
it. My other question is how do I specify the having clause if I put
the single quotes like this: HAVING
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = '500-001' I get an
error. I can again make it work fine in query analyzer but I cant seem
to figure out how to make it work in this stored procedure. Thanks for
all your help!!


Reply With Quote
  #6  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-13-2007 , 04:23 PM



(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = ''500-001''

--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html

"Mike K" <vwttracer (AT) hotmail (DOT) com> wrote

Quote:
On Dec 13, 1:50 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
You declared @strBody as varchar(1000) and in fact your SQL statement is
around 2339 characters (according to a quick copy/paste to a text
editor).
Try declaring as varchar(3000).

HTH,

Plamen Ratchevhttp://www.SQLStudio.com

Thanks that really helped. I glanced over it and yet totally missed
it. My other question is how do I specify the having clause if I put
the single quotes like this: HAVING
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = '500-001' I get an
error. I can again make it work fine in query analyzer but I cant seem
to figure out how to make it work in this stored procedure. Thanks for
all your help!!



Reply With Quote
  #7  
Old   
Mike K
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-13-2007 , 04:24 PM



On Dec 13, 3:23 pm, "Greg D. Moore \(Strider\)"
<mooregr_deletet... (AT) greenms (DOT) com> wrote:
Quote:
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = ''500-001''

--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html

"Mike K" <vwttra... (AT) hotmail (DOT) com> wrote in message

news:2ee535d8-660f-47d9-ba37-cae5734ad537 (AT) d4g2000prg (DOT) googlegroups.com...

On Dec 13, 1:50 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
You declared @strBody as varchar(1000) and in fact your SQL statement is
around 2339 characters (according to a quick copy/paste to a text
editor).
Try declaring as varchar(3000).

HTH,

Plamen Ratchevhttp://www.SQLStudio.com

Thanks that really helped. I glanced over it and yet totally missed
it. My other question is how do I specify the having clause if I put
the single quotes like this: HAVING
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = '500-001' I get an
error. I can again make it work fine in query analyzer but I cant seem
to figure out how to make it work in this stored procedure. Thanks for
all your help!!
I have tried that... i get an invalid column name. Is this a
conversion issue?


Reply With Quote
  #8  
Old   
Mike K
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-13-2007 , 04:57 PM



On Dec 13, 3:24 pm, Mike K <vwttra... (AT) hotmail (DOT) com> wrote:
Quote:
On Dec 13, 3:23 pm, "Greg D. Moore \(Strider\)"



mooregr_deletet... (AT) greenms (DOT) com> wrote:
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = ''500-001''

--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html

"Mike K" <vwttra... (AT) hotmail (DOT) com> wrote in message

news:2ee535d8-660f-47d9-ba37-cae5734ad537 (AT) d4g2000prg (DOT) googlegroups.com...

On Dec 13, 1:50 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
You declared @strBody as varchar(1000) and in fact your SQL statement is
around 2339 characters (according to a quick copy/paste to a text
editor).
Try declaring as varchar(3000).

HTH,

Plamen Ratchevhttp://www.SQLStudio.com

Thanks that really helped. I glanced over it and yet totally missed
it. My other question is how do I specify the having clause if I put
the single quotes like this: HAVING
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = '500-001' I get an
error. I can again make it work fine in query analyzer but I cant seem
to figure out how to make it work in this stored procedure. Thanks for
all your help!!

I have tried that... i get an invalid column name. Is this a
conversion issue?
I just realized what you were trying to say Gregg. Its two single
quotes '' not a double " . That worked. Thanks


Reply With Quote
  #9  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-14-2007 , 08:10 AM



"Mike K" <vwttracer (AT) hotmail (DOT) com> wrote

Quote:
On Dec 13, 3:24 pm, Mike K <vwttra... (AT) hotmail (DOT) com> wrote:
On Dec 13, 3:23 pm, "Greg D. Moore \(Strider\)"



mooregr_deletet... (AT) greenms (DOT) com> wrote:
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = ''500-001''

--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com
http://www.greenms.com/sqlserver.html

"Mike K" <vwttra... (AT) hotmail (DOT) com> wrote in message

news:2ee535d8-660f-47d9-ba37-cae5734ad537 (AT) d4g2000prg (DOT) googlegroups.com...

On Dec 13, 1:50 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
You declared @strBody as varchar(1000) and in fact your SQL
statement is
around 2339 characters (according to a quick copy/paste to a text
editor).
Try declaring as varchar(3000).

HTH,

Plamen Ratchevhttp://www.SQLStudio.com

Thanks that really helped. I glanced over it and yet totally missed
it. My other question is how do I specify the having clause if I put
the single quotes like this: HAVING
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = '500-001' I get an
error. I can again make it work fine in query analyzer but I cant
seem
to figure out how to make it work in this stored procedure. Thanks
for
all your help!!

I have tried that... i get an invalid column name. Is this a
conversion issue?

I just realized what you were trying to say Gregg. Its two single
quotes '' not a double " . That worked. Thanks
Not a problem.

BTW, that's Greg with single G, not double G :-)



--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html




Reply With Quote
  #10  
Old   
Mike K
 
Posts: n/a

Default Re: Trying to perform a query using XP Sendmail - 12-14-2007 , 08:28 AM



On Dec 14, 7:10 am, "Greg D. Moore \(Strider\)"
<mooregr_deletet... (AT) greenms (DOT) com> wrote:
Quote:
"Mike K" <vwttra... (AT) hotmail (DOT) com> wrote in message

news:8af1fbb5-a087-4e8a-a4e3-fe425e198ce1 (AT) e25g2000prg (DOT) googlegroups.com...



On Dec 13, 3:24 pm, Mike K <vwttra... (AT) hotmail (DOT) com> wrote:
On Dec 13, 3:23 pm, "Greg D. Moore \(Strider\)"

mooregr_deletet... (AT) greenms (DOT) com> wrote:
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = ''500-001''

--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com
http://www.greenms.com/sqlserver.html

"Mike K" <vwttra... (AT) hotmail (DOT) com> wrote in message

news:2ee535d8-660f-47d9-ba37-cae5734ad537 (AT) d4g2000prg (DOT) googlegroups.com...

On Dec 13, 1:50 pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
You declared @strBody as varchar(1000) and in fact your SQL
statement is
around 2339 characters (according to a quick copy/paste to a text
editor).
Try declaring as varchar(3000).

HTH,

Plamen Ratchevhttp://www.SQLStudio.com

Thanks that really helped. I glanced over it and yet totally missed
it. My other question is how do I specify the having clause if I put
the single quotes like this: HAVING
(FSA_Weekly_Flash.dbo.GL_Account.GL_Account) = '500-001' I get an
error. I can again make it work fine in query analyzer but I cant
seem
to figure out how to make it work in this stored procedure. Thanks
for
all your help!!

I have tried that... i get an invalid column name. Is this a
conversion issue?

I just realized what you were trying to say Gregg. Its two single
quotes '' not a double " . That worked. Thanks

Not a problem.

BTW, that's Greg with single G, not double G :-)

--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
HAHA it was one of those double kinda day!


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.