Solved

T-SQL Data Type & sp_send_dbmail

Posted on 2012-03-26
6
947 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 500 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell help for creating accounts 283 55
T-SQL: Stored Procedure Syntax 3 34
getting error while running below query  in sql 2 16
access to sql migration 5 24
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…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

726 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