Solved

MSSQL Trigger

Posted on 2011-02-16
6
623 Views
Last Modified: 2012-06-27
I have this trigger that takes the table data from Log_Transactions and Entryaceess and sends out email alerts.  I'm having issues with the table format on the email body, the entire record is crammed into one field.  see attached Screencast that shows the email.

The trigger code:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER trigger [checkinemail] on [INCIDENTTRAX].[dbo].[Log_Transactions]
after insert as
begin

DECLARE @xml nvarchar(MAX)
DECLARE @body nvarchar(MAX)
DECLARE @useremail as nvarchar(100)


SELECT @useremail=e.notifyemail FROM EntryAccess e, inserted i WHERE e.userid=i.hostuserid

SET @xml =CAST(( select FirstName as 'td', LastName as 'td', HostUserId as 'td', CredentialId as 'td', [Description] as 'td', dtDate as 'td', ReaderName as 'td', IncidentID as 'td' FROM inserted FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

SET @body ='<html><H1>INCIDENTTRAX WST, NYC, WST</H1><body bgcolor=yellow><table border = 2><tr><th>FirstName</th><th>LastName</th><th>HostUserId</th><th>CredentialId</th><th>Description</th><th>dtDate</th><th>ReaderName</th><th>IncidentID</th></tr>'
SET @body = @body + @xml +'</table></body></html>'

EXEC msdb.dbo.sp_send_dbmail

@recipients =@useremail,
@body = @body,
@body_format ='HTML',
@subject ='INCIDENTTRAX ALERT!',
@profile_name ='INCIDENTTRAX'

end jtequia-410357.flv
0
Comment
Question by:jtequia
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 34909323
can you post an example of how the XML variable value looks like?
0
 

Author Comment

by:jtequia
ID: 34909362
See attached Sample output
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 34909391
add a print statement in your triggegr
i want to see the html code that is genrated
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 250 total points
ID: 34909750
Just do this:
SET @xml =CAST((SELECT TOP 10 FirstName as 'td', LastName as 'td', HostUserId as 'td', CredentialId as 'td', [Description] as 'td', dtDate as 'td', ReaderName as 'td', IncidentID as 'td' FROM [INCIDENTTRAX].[dbo].[Log_Transactions] FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))

PRINT @Xml

And you will see the problem immediately.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 34909857
In other words, if you run that from a query window (outside the Trigger) you should see the problem.
0
 

Author Closing Comment

by:jtequia
ID: 35000240
Thanks
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VM SQL server license. 1 67
What does "Between" mean? 6 47
T-SQL: need to reset a declared variable 4 33
SqlServer amend PK column 5 12
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

738 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