?
Solved

T-SQL Data Type & sp_send_dbmail

Posted on 2012-03-26
6
Medium Priority
?
968 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
  • 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

850 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