Solved

T-SQL Data Type & sp_send_dbmail

Posted on 2012-03-26
6
933 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry about that, ignore my initial, I changed it now, if you copy the code from the link and then invoke the function.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:Rubicon2009
Comment Utility
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
Comment Utility
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
Comment Utility
Okay Keep me posted :)

I'll give point for the REPLACE solution
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

728 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

9 Experts available now in Live!

Get 1:1 Help Now