Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

T-SQL Data Type & sp_send_dbmail

Posted on 2012-03-26
6
Medium Priority
?
962 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

618 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