Solved

MSSQL Trigger

Posted on 2011-02-16
6
618 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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

895 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

16 Experts available now in Live!

Get 1:1 Help Now