I'm trying to use an INSERT TRIGGER in an SQL Server db. It's my first trigger an I want to use it to send an email if certain value has been met in that inserted row:
CREATE TRIGGER dbo.SEND_MAIL
ON dbo.table
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON
IF (SELECT field1 FROM INSERTED) = '1'
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'mail address',
@profile_name = 'profile1',
@subject = 'subject',
@body = 'body text';
END
END
If I try to execute the send_dbmail stored procedure with those email parameters, the email is sent perfectly. But not in the trigger.
I see that values that comply the condition in the IF statement (field1 = '1') are being inserted in the table. But can't figure out why is not working in the trigger.
I tried to use 'LIKE '1'' instead of equal in the IF but no luck.
Any hint to make it work?
Thank you so much in advance,