Solved

MSSQL Trigger

Posted on 2011-02-16
6
620 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

820 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