TR_MKTSEGMENTATIONSEGMENTACTIVE_AUDIT_UPDATE

Definition

Copy
/* 
Generated by Blackbaud AppFx Platform
Date:  11/11/2014 4:22:20 PM
Assembly Version:  Blackbaud.AppFx.Platform.SqlClr, Version=4.0.2.0, Culture=neutral, PublicKeyToken=null
Copyright Blackbaud
*/
CREATE TRIGGER dbo.TR_MKTSEGMENTATIONSEGMENTACTIVE_AUDIT_UPDATE ON dbo.MKTSEGMENTATIONSEGMENTACTIVE AFTER UPDATE NOT FOR REPLICATION
AS 

declare @ChangeAgentID uniqueidentifier
declare @AuditKey uniqueidentifier
declare @AuditDate datetime
declare @DateChanged datetime

set nocount on

set @DateChanged = null
set @ChangeAgentID = null

-- make sure the datestamp and changeagent fields are updated

if not update(CHANGEDBYID) 
    begin

        --Get a default changeagent id.  Applications should always explicitly included CHANGEDBYID in any updates to avoid a default change agent id.

        exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENTFROMCONTEXT @ChangeAgentID output

        IF not update(DATECHANGED) 
            begin
                -- neither datechanged nor changedbyid were updated, so update both

                set @DateChanged = GetDate()
                update dbo.MKTSEGMENTATIONSEGMENTACTIVE set CHANGEDBYID = @ChangeAgentID, DATECHANGED = @DateChanged from dbo.MKTSEGMENTATIONSEGMENTACTIVE inner join INSERTED on MKTSEGMENTATIONSEGMENTACTIVE.ID = INSERTED.ID
            end
        else
            -- date changed was updated, but changedbyid wasn't so just update changedbyid

            update dbo.MKTSEGMENTATIONSEGMENTACTIVE set CHANGEDBYID = @ChangeAgentID from dbo.MKTSEGMENTATIONSEGMENTACTIVE inner join INSERTED on MKTSEGMENTATIONSEGMENTACTIVE.ID = INSERTED.ID
    end

else if not update(DATECHANGED) 
    begin
        set @DateChanged = GetDate()
        update dbo.MKTSEGMENTATIONSEGMENTACTIVE set DATECHANGED = @DateChanged from dbo.MKTSEGMENTATIONSEGMENTACTIVE inner join INSERTED on MKTSEGMENTATIONSEGMENTACTIVE.ID = INSERTED.ID
    end

--peform the audit

if dbo.UFN_AUDITENABLED('MKTSEGMENTATIONSEGMENTACTIVE') = 1
    begin

        -- the audit key is used to associate the rows in the audit table with a single atomic deletion/modification

        -- we pre-fetch the audit date to make sure both rows contain exactly the same date value.


        set @AuditKey = NewID()
        set @AuditDate = GetDate()

        INSERT INTO dbo.MKTSEGMENTATIONSEGMENTACTIVEAUDIT(
            AUDITRECORDID, 
            AUDITKEY,
            AUDITCHANGEAGENTID,
            AUDITDATE, 
            AUDITTYPECODE,
            [TESTSEGMENTID],
            [QUANTITY],
            [RESPONDERS],
            [RESPONSES],
            [VARIABLECOST],
            [FIXEDCOST],
            [TOTALCOST],
            [COSTPERDOLLARRAISED],
            [TOTALGIFTAMOUNT],
            [AVERAGEGIFTAMOUNT],
            [RESPONSERATE],
            [LIFT],
            [ROIAMOUNT],
            [ROIPERCENT],
            [EXPECTEDRESPONDERS],
            [EXPECTEDTOTALGIFTAMOUNT],
            [EXPECTEDROIAMOUNT],
            [EXPECTEDROIPERCENT],
            [ADDEDBYID],
            [CHANGEDBYID],
            [DATEADDED],
            [DATECHANGED],
            [EXPECTEDCOSTPERDOLLARRAISED],
            [SEGMENTID],
            [IMPRESSIONS],
            [IMPRESSIONCALCULATIONMETHODCODE],
            [ORGANIZATIONVARIABLECOST],
            [ORGANIZATIONFIXEDCOST],
            [ORGANIZATIONTOTALCOST],
            [ORGANIZATIONCOSTPERDOLLARRAISED],
            [ORGANIZATIONTOTALGIFTAMOUNT],
            [ORGANIZATIONAVERAGEGIFTAMOUNT],
            [ORGANIZATIONROIAMOUNT],
            [ORGANIZATIONEXPECTEDCOSTPERDOLLARRAISED],
            [ORGANIZATIONEXPECTEDTOTALGIFTAMOUNT],
            [ORGANIZATIONEXPECTEDROIAMOUNT],
            [BASECURRENCYID],
            [COSTTOACQUIRE],
            [COSTPERTHOUSAND],
            [GROSSPERTHOUSAND],
            [NETPERTHOUSAND],
            [ORGANIZATIONCOSTTOACQUIRE],
            [ORGANIZATIONCOSTPERTHOUSAND],
            [ORGANIZATIONGROSSPERTHOUSAND],
            [ORGANIZATIONNETPERTHOUSAND]
        ) 
        SELECT 
            ID,
            @AuditKey,
            COALESCE(@ChangeAgentID,(SELECT CHANGEDBYID FROM INSERTED WHERE INSERTED.ID=DELETED.ID)), --If explicitly updating CHANGEDBYID, use that, else use the the one default one fetched above

            @AuditDate,
            0, --Before Update

            [TESTSEGMENTID],
            [QUANTITY],
            [RESPONDERS],
            [RESPONSES],
            [VARIABLECOST],
            [FIXEDCOST],
            [TOTALCOST],
            [COSTPERDOLLARRAISED],
            [TOTALGIFTAMOUNT],
            [AVERAGEGIFTAMOUNT],
            [RESPONSERATE],
            [LIFT],
            [ROIAMOUNT],
            [ROIPERCENT],
            [EXPECTEDRESPONDERS],
            [EXPECTEDTOTALGIFTAMOUNT],
            [EXPECTEDROIAMOUNT],
            [EXPECTEDROIPERCENT],
            [ADDEDBYID],
            [CHANGEDBYID],
            [DATEADDED],
            [DATECHANGED],
            [EXPECTEDCOSTPERDOLLARRAISED],
            [SEGMENTID],
            [IMPRESSIONS],
            [IMPRESSIONCALCULATIONMETHODCODE],
            [ORGANIZATIONVARIABLECOST],
            [ORGANIZATIONFIXEDCOST],
            [ORGANIZATIONTOTALCOST],
            [ORGANIZATIONCOSTPERDOLLARRAISED],
            [ORGANIZATIONTOTALGIFTAMOUNT],
            [ORGANIZATIONAVERAGEGIFTAMOUNT],
            [ORGANIZATIONROIAMOUNT],
            [ORGANIZATIONEXPECTEDCOSTPERDOLLARRAISED],
            [ORGANIZATIONEXPECTEDTOTALGIFTAMOUNT],
            [ORGANIZATIONEXPECTEDROIAMOUNT],
            [BASECURRENCYID],
            [COSTTOACQUIRE],
            [COSTPERTHOUSAND],
            [GROSSPERTHOUSAND],
            [NETPERTHOUSAND],
            [ORGANIZATIONCOSTTOACQUIRE],
            [ORGANIZATIONCOSTPERTHOUSAND],
            [ORGANIZATIONGROSSPERTHOUSAND],
            [ORGANIZATIONNETPERTHOUSAND]

        FROM DELETED


        INSERT INTO dbo.MKTSEGMENTATIONSEGMENTACTIVEAUDIT(
            AUDITRECORDID, 
            AUDITKEY,
            AUDITCHANGEAGENTID,
            AUDITDATE, 
            AUDITTYPECODE,
            [TESTSEGMENTID],
            [QUANTITY],
            [RESPONDERS],
            [RESPONSES],
            [VARIABLECOST],
            [FIXEDCOST],
            [TOTALCOST],
            [COSTPERDOLLARRAISED],
            [TOTALGIFTAMOUNT],
            [AVERAGEGIFTAMOUNT],
            [RESPONSERATE],
            [LIFT],
            [ROIAMOUNT],
            [ROIPERCENT],
            [EXPECTEDRESPONDERS],
            [EXPECTEDTOTALGIFTAMOUNT],
            [EXPECTEDROIAMOUNT],
            [EXPECTEDROIPERCENT],
            [ADDEDBYID],
            [CHANGEDBYID],
            [DATEADDED],
            [DATECHANGED],
            [EXPECTEDCOSTPERDOLLARRAISED],
            [SEGMENTID],
            [IMPRESSIONS],
            [IMPRESSIONCALCULATIONMETHODCODE],
            [ORGANIZATIONVARIABLECOST],
            [ORGANIZATIONFIXEDCOST],
            [ORGANIZATIONTOTALCOST],
            [ORGANIZATIONCOSTPERDOLLARRAISED],
            [ORGANIZATIONTOTALGIFTAMOUNT],
            [ORGANIZATIONAVERAGEGIFTAMOUNT],
            [ORGANIZATIONROIAMOUNT],
            [ORGANIZATIONEXPECTEDCOSTPERDOLLARRAISED],
            [ORGANIZATIONEXPECTEDTOTALGIFTAMOUNT],
            [ORGANIZATIONEXPECTEDROIAMOUNT],
            [BASECURRENCYID],
            [COSTTOACQUIRE],
            [COSTPERTHOUSAND],
            [GROSSPERTHOUSAND],
            [NETPERTHOUSAND],
            [ORGANIZATIONCOSTTOACQUIRE],
            [ORGANIZATIONCOSTPERTHOUSAND],
            [ORGANIZATIONGROSSPERTHOUSAND],
            [ORGANIZATIONNETPERTHOUSAND]
        ) 
        SELECT 
            ID,
            @AuditKey,
            COALESCE(@ChangeAgentID,CHANGEDBYID), --If explicitly updating CHANGEDBYID, use that, else use the the one default one fetched above

            @AuditDate,
            1, --After Update

            [TESTSEGMENTID],
            [QUANTITY],
            [RESPONDERS],
            [RESPONSES],
            [VARIABLECOST],
            [FIXEDCOST],
            [TOTALCOST],
            [COSTPERDOLLARRAISED],
            [TOTALGIFTAMOUNT],
            [AVERAGEGIFTAMOUNT],
            [RESPONSERATE],
            [LIFT],
            [ROIAMOUNT],
            [ROIPERCENT],
            [EXPECTEDRESPONDERS],
            [EXPECTEDTOTALGIFTAMOUNT],
            [EXPECTEDROIAMOUNT],
            [EXPECTEDROIPERCENT],
            [ADDEDBYID],
            coalesce(@ChangeAgentID, [CHANGEDBYID]),
            [DATEADDED],
            coalesce(@DateChanged, [DATECHANGED]),
            [EXPECTEDCOSTPERDOLLARRAISED],
            [SEGMENTID],
            [IMPRESSIONS],
            [IMPRESSIONCALCULATIONMETHODCODE],
            [ORGANIZATIONVARIABLECOST],
            [ORGANIZATIONFIXEDCOST],
            [ORGANIZATIONTOTALCOST],
            [ORGANIZATIONCOSTPERDOLLARRAISED],
            [ORGANIZATIONTOTALGIFTAMOUNT],
            [ORGANIZATIONAVERAGEGIFTAMOUNT],
            [ORGANIZATIONROIAMOUNT],
            [ORGANIZATIONEXPECTEDCOSTPERDOLLARRAISED],
            [ORGANIZATIONEXPECTEDTOTALGIFTAMOUNT],
            [ORGANIZATIONEXPECTEDROIAMOUNT],
            [BASECURRENCYID],
            [COSTTOACQUIRE],
            [COSTPERTHOUSAND],
            [GROSSPERTHOUSAND],
            [NETPERTHOUSAND],
            [ORGANIZATIONCOSTTOACQUIRE],
            [ORGANIZATIONCOSTPERTHOUSAND],
            [ORGANIZATIONGROSSPERTHOUSAND],
            [ORGANIZATIONNETPERTHOUSAND]

        FROM INSERTED

    end