Solved

sp_send_dbmail  problem

Posted on 2009-04-06
3
378 Views
Last Modified: 2012-05-06
I'm having a problem with adding a local variable into my body of a html email sent using the sp_send_dbmail .  My code is below at the moment I'm getting this error


"The data types text and nvarchar are incompatible in the add operator."
alter procedure sp_1
 

@email nvarchar(50)

as
 

declare @username nvarchar(50)

declare @password nvarchar(50)

declare @email2 nvarchar(50)

declare @html nvarchar(max)
 
 

set @email2 = (select email from aspnet_membership where email = @email)

set @username = (select username from aspnet_Users u join aspnet_membership m on m.userid = u.userid where m.email = @email)

set @password = (select password from aspnet_membership where email = @email)

set @html = 'some html code' + @username + 'some html code';

 exec msdb.dbo.sp_send_dbmail 

		@profile_name='emailprofile', 

		@recipients= @email2 ,

		@subject= "Password",

		@body = @html,

		@body_format = html

Open in new window

0
Comment
Question by:leap29
  • 2
3 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24078574
Try replacing the below statement

set @html = 'some html code' + @username + 'some html code';

To

set @html = N'some html code' + cast(@username as Nvarchar) + N'some html code';

This should help you out
0
 

Author Comment

by:leap29
ID: 24078855
Thanks for the relpy, I've done what you suggested but I'm still getting the same error message.

The data types ntext and nvarchar are incompatible in the add operator.
 

DECLARE @HTML  NVARCHAR(MAX)
 

@html = N'some html code' + cast(@username as Nvarchar) + N'some html code';

Open in new window

0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 250 total points
ID: 24078998
Ok, Try this one out:

Make sure that Username in the table aspnet_Users is of varchar datatype.
alter procedure sp_1

 

@email nvarchar(50)

as

declare @username nvarchar(50),

        @password nvarchar(50),

        @email2 nvarchar(50),

        @html nvarchar(max);

 

set @email2 = (select email from aspnet_membership where email = @email)

set @username = (select username from aspnet_Users u join aspnet_membership m on m.userid = u.userid where m.email = @email)

set @password = (select password from aspnet_membership where email = @email)

set @html = N'some html code' + cast(@username as Nvarchar) + N'some html code';

 exec msdb.dbo.sp_send_dbmail 

                @profile_name='emailprofile', 

                @recipients= @email2 ,

                @subject= "Password",

                @body = @html,

                @body_format = 'HTML'

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now