TR_FEPPROCESS_AUDIT_UPDATE

Definition

Copy
/* 
Generated by Blackbaud AppFx Platform
Date:  4/29/2010 7:19:27 PM
Assembly Version:  Blackbaud.AppFx.Platform.SqlClr, Version=2.6.1444.0, Culture=neutral, PublicKeyToken=null
Copyright Blackbaud
*/
CREATE TRIGGER dbo.TR_FEPPROCESS_AUDIT_UPDATE ON dbo.FEPPROCESS 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.FEPPROCESS set CHANGEDBYID = @ChangeAgentID, DATECHANGED = @DateChanged from dbo.FEPPROCESS inner join INSERTED on FEPPROCESS.ID = INSERTED.ID
            end
        else
            -- date changed was updated, but changedbyid wasn't so just update changedbyid

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

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

--peform the audit

if dbo.UFN_AUDITENABLED('FEPPROCESS') = 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.FEPPROCESSAUDIT(
            AUDITRECORDID, 
            AUDITKEY,
            AUDITCHANGEAGENTID,
            AUDITDATE, 
            AUDITTYPECODE,
            [NAME],
            [DATELASTRUN],
            [DESCRIPTION],
            [SITEID],
            [ORGANIZATIONNAME],
            [COUNTRYID],
            [POSTCODE],
            [IDNUMBER],
            [CONTACTNAME],
            [CONTACTEMAIL],
            [CONTACTPHONE],
            [AFP],
            [CASE],
            [AHP],
            [ALDE],
            [NCPG],
            [CRD],
            [OTHER],
            [OTHERAFFILIATION],
            [INITIALYEAR],
            [SUBSECTORCODE],
            [FUNDRAISINGEXPENSES],
            [FTEPROFESSIONAL],
            [FTESUPPORT],
            [FTECONSULTANT],
            [FTEVOLUNTEER],
            [COMMENTS],
            [ADDEDBYID],
            [CHANGEDBYID],
            [DATEADDED],
            [DATECHANGED]
        ) 
        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

            [NAME],
            [DATELASTRUN],
            [DESCRIPTION],
            [SITEID],
            [ORGANIZATIONNAME],
            [COUNTRYID],
            [POSTCODE],
            [IDNUMBER],
            [CONTACTNAME],
            [CONTACTEMAIL],
            [CONTACTPHONE],
            [AFP],
            [CASE],
            [AHP],
            [ALDE],
            [NCPG],
            [CRD],
            [OTHER],
            [OTHERAFFILIATION],
            [INITIALYEAR],
            [SUBSECTORCODE],
            [FUNDRAISINGEXPENSES],
            [FTEPROFESSIONAL],
            [FTESUPPORT],
            [FTECONSULTANT],
            [FTEVOLUNTEER],
            [COMMENTS],
            [ADDEDBYID],
            [CHANGEDBYID],
            [DATEADDED],
            [DATECHANGED]

        FROM DELETED


        INSERT INTO dbo.FEPPROCESSAUDIT(
            AUDITRECORDID, 
            AUDITKEY,
            AUDITCHANGEAGENTID,
            AUDITDATE, 
            AUDITTYPECODE,
            [NAME],
            [DATELASTRUN],
            [DESCRIPTION],
            [SITEID],
            [ORGANIZATIONNAME],
            [COUNTRYID],
            [POSTCODE],
            [IDNUMBER],
            [CONTACTNAME],
            [CONTACTEMAIL],
            [CONTACTPHONE],
            [AFP],
            [CASE],
            [AHP],
            [ALDE],
            [NCPG],
            [CRD],
            [OTHER],
            [OTHERAFFILIATION],
            [INITIALYEAR],
            [SUBSECTORCODE],
            [FUNDRAISINGEXPENSES],
            [FTEPROFESSIONAL],
            [FTESUPPORT],
            [FTECONSULTANT],
            [FTEVOLUNTEER],
            [COMMENTS],
            [ADDEDBYID],
            [CHANGEDBYID],
            [DATEADDED],
            [DATECHANGED]
        ) 
        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

            [NAME],
            [DATELASTRUN],
            [DESCRIPTION],
            [SITEID],
            [ORGANIZATIONNAME],
            [COUNTRYID],
            [POSTCODE],
            [IDNUMBER],
            [CONTACTNAME],
            [CONTACTEMAIL],
            [CONTACTPHONE],
            [AFP],
            [CASE],
            [AHP],
            [ALDE],
            [NCPG],
            [CRD],
            [OTHER],
            [OTHERAFFILIATION],
            [INITIALYEAR],
            [SUBSECTORCODE],
            [FUNDRAISINGEXPENSES],
            [FTEPROFESSIONAL],
            [FTESUPPORT],
            [FTECONSULTANT],
            [FTEVOLUNTEER],
            [COMMENTS],
            [ADDEDBYID],
            coalesce(@ChangeAgentID, [CHANGEDBYID]),
            [DATEADDED],
            coalesce(@DateChanged, [DATECHANGED])

        FROM INSERTED

    end