How to start a trigger after SPROC is complete?

I wrote a trigger that sends email after the INSERT line is executed .

ALTER TRIGGER TR_SendMailOnDataRequest
      ON DataRequest
AFTER INSERT
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE 
        @DR_Id INT,
        @DR_FullName VARCHAR(200),
        @DR_Email VARCHAR(200),
        @DR_Phone VARCHAR(20),
        @UT_Name VARCHAR(50),
        @DR_UserTypeOther VARCHAR(50) = NULL,
        @D_Name VARCHAR(200),
        @DR_RequestDate DATETIME,
        @UF_LinkedFiles VARCHAR(MAX),
        @DRN_Names VARCHAR(200),
        @DR_Description VARCHAR(1200),
        @DR_CreatedOn DATETIME,
        @analystMailList VARCHAR(MAX),
        @tableHtml NVARCHAR(MAX),
        @downloadLink VARCHAR(MAX) = N'NONE'

    SELECT @DR_Id = MAX(DR_Id) FROM dbo.DataRequest

    SELECT 
        @DR_FullName = DR_FullName,
        @DR_Email = DR_Email,
        @DR_Phone = DR_Phone,
        @UT_Name = UT_Name,
        @DR_UserTypeOther = DR_UserTypeOther,
        @D_Name = D_Name,
        @DR_RequestDate = DR_RequestDate,
        @UF_LinkedFiles = UF_LinkedFiles,
        @DRN_Names = DRN_Names,
        @DR_Description = DR_Description,
        @DR_CreatedOn = DR_CreatedOn
    FROM
        dbo.FN_GetDataRequest(@DR_Id)

    SELECT @analystMailList = dbo.FN_GetAnalystsMailList()

    IF (LEN(@UF_LinkedFiles) > 0)
    BEGIN
        SET @downloadLink = N'<a href="http://localhost:8500/workrequest/index.cfm?event=downloads.index&id=' + CAST(@DR_Id AS VARCHAR(10)) + N'&k='+ SUBSTRING(master.dbo.fn_varbintohexstr(HashBytes('SHA', ':be9[Dcv9wF~W!?xx4JO0OXLbZ@0p4+[~z0dO|:U,OF!13^xZb')), 3, 32) + N'">Downloads</a>'
    END

    SET @tableHTML =
        N'<H1>Data Request</H1>' +
        N'<UL>' +
        N'<LI>Full Name: ' + @UF_LinkedFiles + N'</LI>' +
        N'<LI>Email: ' + @DR_Email + N'</LI>' +
        N'<LI>Phone: ' + CAST(@DR_Phone AS VARCHAR(20)) + N'</LI>' +
        N'<LI>User Type: ' + @UT_Name + N'</LI>' +
        N'<LI>User Type Other: ' + COALESCE(@DR_UserTypeOther, N'NONE') + N'</LI>' +
        N'<LI>Reuest Date: ' + CONVERT(VARCHAR(20), @DR_RequestDate, 107) + N'</LI>' +
        N'<LI>Downloads: ' + @downloadLink + N'</LI>' +
        N'</UL>';

    BEGIN
        EXEC msdb.dbo.sp_send_dbmail 
            @profile_name = 'Example',
            @recipients = 'John Doe<jdoe@example>',
            --@recipients = @analystMailList,
            @reply_to = @DR_Email,
            @subject = 'Email Test',
            @body_format = 'HTML',
            @body = @tableHtml
    END
END
GO

The above trigger is fired when ROW INSERT is running in the DataRequest table . After the row insert operation, I take the IDENTITY element generated after the INSERT operation and use it as a foreign key, and INSERT - other values ​​in another table. Finally, I use the values ​​from both tables and create an email to send.

(, @UF_LinkedFiles), , TRIGGER INSERT FIRST, INSERT SECOND, EMAIL.

, , TRIGGER , SPROC, INSERT , .

- Tables

+4
2

EMAIL SENDING SPROC, .

+2

, , , . , SELECT , , .

,

tblProgress
    id integer,
    fieldA integer,
    fieldB integer,
    fieldC integer

, 3 TableA, TableB TableC, INSERT , tblProgress.

TableA TableB TableC.

tblProgress AFTER UPDATE, 3 , <<24 >

3 , .

+1

Source: https://habr.com/ru/post/1545427/


All Articles