Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Unable to use a variable in the @body parameter of sp_send_dbmail Expand / Collapse
Author
Message
Posted Tuesday, July 2, 2013 1:51 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, August 6, 2013 6:51 AM
Points: 2, Visits: 21
I am trying to build a variable to be used with the @body parameter of sp_send_dbmail. Here is the code I am trying to run:
DECLARE @v_body varchar(MAX)
DECLARE @v_business_name varchar(75) = 'Hollies Gift Shop'
DECLARE @v_owner_first_name varchar(30) = 'Bernice'
DECLARE @v_owner_last_name varchar(30) = 'Goodly'
DECLARE @v_profile varchar(30) = 'testprofile'

set @v_body = 'Business Name: ' + @v_business_name + char(13) +char(10)
+ 'First Name : ' + @v_owner_first_name + char(13) +char(10)
+ 'Last Name : ' + @v_owner_first_name
USE msdb
GO
EXEC sp_send_dbmail
@profile_name = 'testprofile',
@recipients = 'example@yahoo.com',
@subject = 'Test message',
@body = @v_body

I receive the following error: Must declare the scalar variable "@v_body".
What is it that I'm doing wrong

Post #1469732
Posted Tuesday, July 2, 2013 1:59 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:00 PM
Points: 4,196, Visits: 3,235
You have a GO after your USE statement. This terminates the batch above the GO, eliminating the variable declaration in the batch started immediately after the GO. Move the USE and GO above your declarations and all should be well.

USE msdb
GO

DECLARE @v_body varchar(MAX)
DECLARE @v_business_name varchar(75) = 'Hollies Gift Shop'
DECLARE @v_owner_first_name varchar(30) = 'Bernice'
DECLARE @v_owner_last_name varchar(30) = 'Goodly'
DECLARE @v_profile varchar(30) = 'testprofile'

set @v_body = 'Business Name: ' + @v_business_name + char(13) +char(10)
+ 'First Name : ' + @v_owner_first_name + char(13) +char(10)
+ 'Last Name : ' + @v_owner_first_name

EXEC sp_send_dbmail
@profile_name = 'testprofile',
@recipients = 'example@yahoo.com',
@subject = 'Test message',
@body = @v_body;

Another way is to EXECUTE msdb.dbo.sp_send_dbmail instead. I personally prefer this approach, but it's just a matter of preference.



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1469734
Posted Tuesday, July 2, 2013 2:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, August 6, 2013 6:51 AM
Points: 2, Visits: 21
Ed,
Thank you so much for the help. It worked like a charm and once you explained what was wrong it made perfect sense.
Post #1469738
Posted Tuesday, July 2, 2013 6:20 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 1:00 PM
Points: 4,196, Visits: 3,235
Glad I could help.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1469805
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse