0

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;


5
  • 1
    This does not look like a trigger. It never uses inserted/deleted, and it misses CREATE TRIGGER, so we don't know if it's really trigger Commented Nov 10, 2020 at 9:03
  • Hi take a look at the amendments Commented Nov 10, 2020 at 9:12
  • 1
    Your trigger is "INSTEAD OF INSERT". As this name implies - it runs instead of insert, so actual insert doesn't occur. Commented Nov 10, 2020 at 9:14
  • Hey i just found a solution @Evk , the statement "INSTEAD OF INSERT" was the detriment Commented Nov 10, 2020 at 10:40
  • Note that you should access rows being inserted via "INSERTED" reference, not like you are doing now. Commented Nov 10, 2020 at 10:42

2 Answers 2

2

Your are creating the trigger making it replace the INSERT statement, as long as your trigger does notify the insertion, it should be run AFTER the insert has happened.

You can do further reading on the trigger types in the MS doc

ALTER trigger [dbo].[LPO_ON_INSERT] 
ON [dbo].[tblOrdersDocP_POApprovals]
AFTER INSERT AS
Sign up to request clarification or add additional context in comments.

Comments

0

Hey i just found a solution @Evk , the statement "INSTEAD OF INSERT" was the detriment

1 Comment

pretty much what I told you in my answer, glad you found the problem. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.