When I try to insert a new record into the table I created the trigger on it does not create.
This is after I activated the trigger. When I deactivate it then repeated the insert, it goes through. What am I missing in my trigger?
My trigger identifies all the new rows in a table then emails them to a recipient.
This is my trigger
USE [paldbCAT_TEST]
GO
/****** Object: Trigger [dbo].[LPO_ON_INSERT] Script Date: 10-11-2020 12:09:01 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[LPO_ON_INSERT]
ON [dbo].[tblOrdersDocP_POApprovals]
INSTEAD OF INSERT AS
Declare
@HTMLbody nvarchar (max),
@strOrdPDocID varchar(100),
@strSelectedApproverUserName varchar(50),
@SQL nvarchar(max)
SET @SQL =''
Set @HTMLbody = '<html><head><title> System Tables</title></head><body>'
Set @HTMLbody = @HTMLbody + '<center> <h1 style="font-family: arial, sans-serif">Purchase Requisition(s) awaiting approvals.</h1></center>'
Set @HTMLbody = @HTMLbody + '<center><table style="font-family: arial, sans-serif;border-collapse: collapse;width: 100%;" ><TH style="background-color:black; color:white;border: 1px solid #dddddd;text-align: left;padding: 8px;">Document Number</TH><TH style="background-color:black; color:white;border: 1px solid #dddddd;text-align: left;padding: 8px;">Document Approver</TH>'
declare SystemTable cursor
for
select strOrdPDocID,strSelectedApproverUserName from tblOrdersDocP_POApprovals
open SystemTable
Fetch next from SystemTable into @strOrdPDocID,@strSelectedApproverUserName
While (@@FETCH_STATUS<> -1)
Begin
Set @SQL = @SQL + '<TR><TD style=" border: 1px solid #dddddd;text-align: left;padding: 8px;">' +@strOrdPDocID + '</TD><TD style=" border: 1px solid #dddddd;text-align: left;padding: 8px;">' + @strSelectedApproverUserName + '</TD></TR>'
Fetch next from SystemTable into @strOrdPDocID,@strSelectedApproverUserName
End
Close SystemTable
DEALLOCATE SystemTable
Set @HTMLbody = @HTMLbody + @SQL + '</table></center></body></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'CatNotificationsMain',
@recipients = '[email protected]',
@subject = 'Database Record Deleted',
@body_format='HTML',
@body = @HTMLbody,
@attach_query_result_as_file = 0;