🎉🔥Yo creo este ejemplo de un trigger donde cada vez que se insertara un usuario me iba a llegar un correo en tabla de cada usuario registrado, para esto el requerimiento es haber configurado si o si sp_send_dbmail y agregarle los parametros en donde se ejecuta el profile y el correo 💚ya agregue una columna llamada load_date para verificar el ultimo(s) usuarios nuevos dentro de la tabla UsuarioTarget
CREATE TRIGGER [dbo].[send_email_insert_usuario_new]
ON [dbo].[UsuarioTarget]
FOR INSERT
AS
BEGIN
SET NOCOUNT ON;
SET ANSI_WARNINGS OFF
DECLARE @database NVARCHAR(MAX) = N''
DECLARE @tableHTML nvarchar(max)
DECLARE @Table NVARCHAR(MAX) = N''
DECLARE @subject_content NVARCHAR(MAX) = N''
SELECT @Table = @Table +'<tr style="background-color:'+CASE WHEN (ROW_NUMBER() OVER (ORDER BY id))%2 =1 THEN '#A3E0FF' ELSE '#8ED1FB' END +';">' +
'<td>' + CAST(CONVERT(varchar, CAST(RTRIM((id)) AS varchar), 1) AS varchar) + '</td>' +
'<td>' + CAST(CONVERT(varchar, CAST(RTRIM((name)) AS varchar), 1) AS varchar) + '</td>' +
'<td>' + CAST(CONVERT(varchar, CAST((load_date) AS date), 1) AS varchar) + '</td>' + '</tr>'
FROM [dbo].UsuarioTarget
WHERE (load_date > GETDATE() - 1)
select @database = (select DB_NAME() AS [Current Database])
SET @subject_content = 'User Record Inserted in ' + @database
SET @tableHTML =
N'<H3>Catalogue Users</H3>' +
N'<table border="1" align="center" cellpadding="2" cellspacing="0" style="font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;text-align:center; border-collapse: collapse; width: 100%;" >' +
N'<tr style ="font-size: 14px;font-weight: normal;background: #b9c9fe;">
<th>User ID</th>
<th>Name User</th>
<th>Date</th>' + @Table + N'</table>' + N'<br>' + N'<br>'
print @database
IF EXISTS (SELECT MAX(load_date) FROM UsuarioTarget )
BEGIN
EXEC msdb.dbo.sp_send_dbmail
@recipients = '[email protected]',
@profile_name = 'Email',
@subject = @subject_content,
@body = @tableHTML,
@body_format = 'HTML' ;
END
END
go
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?