sp_send_dbmail

Hi
I have configured dbmail on sql server 2008. This works fine but I wish to Pass dynamic data into the mail message. My stored procedure has a variable @mailtxt  and the variable is populated from other variables as below
declare @Mailtxt nvarchar(250)
declare @email nvarchar(70)
declare @Jobnum nvarchar(12)
select @Jobnum = (select DwJobnumber from inserted)
select @email = (SELECT ‘myemail@mycomp.co.uk’)
select @mailtxt  = (select 'User ' + @email + ' has put bad time value in Job ' + @Jobnum)
EXEC msdb.dbo.sp_send_dbmail @profile_name='Tracker_Mail',
@recipients='mail@mycompany.co.uk',
@subject= 'Bad Delivery date' ,
@body = @mailtxt

The mail is sent but without the details in @mailtxt. What is the correct method to pass the values into the mail body?

Regards
JoeBo
LVL 1
JoeBo747Asked:
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.

Carl TawnSystems and Integration DeveloperCommented:
An initial guess would be that one of your variables contains NULL which is causing @mailtxt to return an empty value.

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
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Try using:

set @mailtxt  = 'User ' + isnull(@email,0) + ' has put bad time value in Job ' + isnull(@Jobnum,0)
wittysloganCommented:
try print @mailtxt  to seet what is being passed to dbmail
kunalapCommented:
i just ran this on my server and it worked just fine. try this:

declare @Mailtxt nvarchar(250)
declare @email nvarchar(70)
declare @Jobnum nvarchar(12)
select @Jobnum = (select DwJobnumber from inserted)
set @email = ‘myemail@mycomp.co.uk’
set @mailtxt  = 'User ' + @email + ' has put bad time value in Job ' + @Jobnum
EXEC msdb.dbo.sp_send_dbmail @profile_name='Tracker_Mail',
@recipients='mail@mycompany.co.uk',
@subject= 'Bad Delivery date' ,
@body = @mailtxt

the only change i made for for Jobnum. so if it still doesn't work, u know Jobnum is the problem.
JoeBo747Author Commented:
Hi Thanks for the assistance all your posts were helpful I have marked according to the amount of help the post gave me.
Regards
JoeBo
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

From novice to tech pro — start learning today.