?
Solved

T-SQL Data Type & sp_send_dbmail

Posted on 2012-03-26
6
Medium Priority
?
957 Views
Last Modified: 2012-03-26
Hi !

I'm having a little issue with sp_send_dbmail and datatype

I've done a trigger that send me an email each time a row is insterted in a table. The email include the inserted data. That's working well.

The table I use contain a column named "subject" and another one name "notes"
My Database collation is : Latin1_General_CI_AS
"Subject" column datatype is : nvarchar(255)
"Notes" column datatype is : ntext

Here's my trigger code:

USE [MyDatabase]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[tr_send_email_when_cancelled]
ON [dbo].[tasks]
AFTER INSERT
AS
      IF EXISTS (SELECT NULL FROM INSERTED WHERE CategoryID = '7F1335C8-5850-4C49-92F4-6D2321B79138')
BEGIN

            DECLARE @UserID Uniqueidentifier = (SELECT ProjectID FROM INSERTED)
            DECLARE @BodyTXT nvarchar(max) = dbo.fnGetHRAnalysis(@UserID,null)
            DECLARE @SubjectTXT VARCHAR(255) = (SELECT subject FROM INSERTED)

            EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'MyProfile',
        @recipients = 'alias@mydomain.ca',
        @body_format = 'HTML',
        @body = @BodyTXT,
        @subject = @SubjectTXT;
      
END


My problem is that each time there is an accented caracter in the "Subject" field (é à è...), I get the email with a scrambled subject like this in outlook :
 "=?utf-8?B?Q2FuY2VsUGxhY2VtZW50IGRlIHRyYXZhaWw6IHZlbmRyZWRpIDI3IGF2cmlsIDIwMTIsIENsaWVudDogQ0xJTklRVUUgTcOJRElDQUxFIERFIExBIEdBUkUsIETDqXBhcnRlbWVudDogUHLDqWzDqHZlbWVudCwgUXVhcnQ6IDA3OjMwOjAwIC0gMTI6MDA6MDAgPSAwNDozMDowMCwgRW1wbG95w6k6IE5hZGVhdSBHZWxpbm..."

email sent with subject without accent are printed correctly in outlook.

Can someone tell me what I did wrong and how to fix it ?
0
Comment
Question by:Rubicon2009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 9

Accepted Solution

by:
OCDan earned 2000 total points
ID: 37767809
This link should help: Accent Removal

There are a few functions on there that can help you resolve this problem. I have used them before quite a bit.
0
 

Author Comment

by:Rubicon2009
ID: 37767859
Msg 156, Level 15, State 1, Procedure tr_send_email_when_cancelled, Line 19
Incorrect syntax near the keyword 'COLLATE'.

DECLARE @SubjectTXT NVARCHAR(255) = 'Un quart de travail à été annulé : ' + (SELECT subject FROM INSERTED)

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MyProfile',
@recipients = alias@domain.ca',
@body_format = 'HTML',
@body = @BodyTXT,
@subject COLLATE LATIN1_GENERAL_CS_AI = @SubjectTXT;
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37767880
Sorry about that, ignore my initial, I changed it now, if you copy the code from the link and then invoke the function.
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 

Author Comment

by:Rubicon2009
ID: 37767996
The solution provided is great but I'll like to know why this is happening. Do you have any ideas ?

I know for sure that @subject used into dbmail is expecting a nvarchar(255) datatype.
Is it possible that my own variable containg the subject is sending more bytes and it is being truncated so the email client can't read it correctly ?
0
 
LVL 9

Expert Comment

by:OCDan
ID: 37768040
I'm not too certain why this is happening but I'll do some investigation and some tests on my system.

I don't think that datalength is the issue, but just try changing datatype to varchar(max) and see if that works ok.
0
 

Author Comment

by:Rubicon2009
ID: 37768071
Okay Keep me posted :)

I'll give point for the REPLACE solution
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

764 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