Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MSSQL Trigger

Posted on 2011-02-16
6
Medium Priority
?
629 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
  • 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
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.

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 1000 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

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

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 article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

927 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