I need to create a trigger
in an SQL table to send an email if the inserted record meets certain conditions.
That is, I create the trigger
in Tabla1
to send an email to X if in the inserted register the IdCircuito= 53
, the IdTipoDoc = 45
and the Gestor = 'Gest1'
. Also, in the body of the email message I want to show the value of a certain field of that inserted record.
I have done something like that but the trigger
is always executed regardless of the inserted record:
CREATE TRIGGER dbo.SendEmail
ON dbo.TitulosDoc
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT 1 FROM TitulosDoc WHERE IdCircuito = 53 AND IdTipoDoc = 45 AND Gestor = 'Gest1')
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@recipients = '[email protected]',
@subject = 'Requerimiento generado',
@body = 'Se ha generado un nuevo requerimiento: ';
END
END
GO
In the body is where I want the text to be displayed with the value of the inserted record field:
@body = 'Se ha generado un nuevo requerimiento: ' + TitulosDoc.NombreDocumento;
Can someone help me?