Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Unable to use a variable in the @body parameter of sp_send_dbmail


Unable to use a variable in the @body parameter of sp_send_dbmail

Author
Message
Slinky1121
Slinky1121
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 35
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
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10334 Visits: 9604
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
Slinky1121
Slinky1121
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 35
Ed,
Thank you so much for the help. It worked like a charm and once you explained what was wrong it made perfect sense.
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10334 Visits: 9604
Glad I could help.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search