Forum Discussion

BMichelle's avatar
BMichelle
Copper Contributor
Jul 02, 2025

Trigger is hanging up the system.

Hi,

I need to send out a database email when the status field is <> '0' for a newly inserted record. I have a trigger that works elsewhere, but is hanging up the system at this location. When I say "hanging up the system" I am referring to the database not populating. The database will populate after the trigger is disabled and another insert occurs. Below is the code for the trigger.

I have checked and the test database email went out and was received. I have also successfully sent out and received an email from a query using  just the Line EXEC msdb.dbo.sp-send-dbmail and the lines that follow from below. It is something with the trigger is all I can come up with.   

I would greatly appreciate any input.

USE [AK_Mid_TV]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER TRIGGER [dbo].[TV Front Image Alarm Alerts]

   ON  [AK_Mid_TV].[dbo].[TV Data]

   FOR INSERT

AS

    SET NOCOUNT ON;

        DECLARE @tableHTML NVARCHAR(MAX);

        SET @tableHTML =

            N'<h1>TV FRONT ALARM ALERT</H1>' +

            N'<table border = "1">' +

            N'<tr><th>Car ID</th><th>Image Time</th>' +

            N'<th>Front Alarm Level</th><th>Front Alarm Temp</th><th>Direction</th>' +



            CAST ( ( SELECT td = dbo.[TV Data].[Car ID], ' ',

                            td = dbo.[TV Data].[Image Time], ' ',

                            [td/@align] = 'center',

                            td = dbo.[TV Data].[Front Image Alarm Status], ' ',

                            [td/@align] = 'center',

                            td = format(dbo.[TV Data].[Front Temp F], '#,#'), ' ',

                            [td/@align] = 'center',

                            td = dbo.[TV Data].[Direction Label]

                       

                        FROM dbo.[TV Data]

                        where [Image Time] in (SELECT MAX([Image Time]) from dbo.[TV Data]) and [Front Image Alarm Status] <> '0'

                        FOR XML PATH ('tr'), TYPE

                        ) AS NVARCHAR(MAX) ) +

                        N'</table>';



                        If @tableHTML <> ' '

                        EXEC msdb.dbo.sp_send_dbmail

                        profile_name = 'Alarm emails',

                        @recipients ='email address removed for privacy reasons',

                        @copy_recipients = 'email address removed for privacy reasons',

                        @subject = 'TV Alarm Alert',

                        Body = @tableHtml,

                        Body_format = 'HTML';

 

1 Reply

  • navindevan's avatar
    navindevan
    Copper Contributor

    Hi BMichelle,

    This is a duplicate question, the solution is already provied.

    https://techcommunity.microsoft.com/discussions/sql_server/trigger-is-hanging-up-the-database/4429895/replies/4430851

    Please mark it as solved.

    Thanks!

Resources