![]() |
---|
/* Generated by Blackbaud AppFx Platform Date: 11/17/2010 10:11:45 AM Assembly Version: Blackbaud.AppFx.Platform.SqlClr, Version=2.8.1975.0, Culture=neutral, PublicKeyToken=null Copyright Blackbaud */ CREATE TRIGGER dbo.TR_ACCOUNTCODE_AUDIT_UPDATE ON dbo.ACCOUNTCODE 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.ACCOUNTCODE set CHANGEDBYID = @ChangeAgentID, DATECHANGED = @DateChanged from dbo.ACCOUNTCODE inner join INSERTED on ACCOUNTCODE.ID = INSERTED.ID end else -- date changed was updated, but changedbyid wasn't so just update changedbyid update dbo.ACCOUNTCODE set CHANGEDBYID = @ChangeAgentID from dbo.ACCOUNTCODE inner join INSERTED on ACCOUNTCODE.ID = INSERTED.ID end else if not update(DATECHANGED) begin set @DateChanged = GetDate() update dbo.ACCOUNTCODE set DATECHANGED = @DateChanged from dbo.ACCOUNTCODE inner join INSERTED on ACCOUNTCODE.ID = INSERTED.ID end --peform the audit if dbo.UFN_AUDITENABLED('ACCOUNTCODE') = 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.ACCOUNTCODEAUDIT( AUDITRECORDID, AUDITKEY, AUDITCHANGEAGENTID, AUDITDATE, AUDITTYPECODE, [CONTRAACCOUNT], [CASHFLOWCODEID], [WORKINGCAPITALCODEID], [STATUSCODEID], [SITEID], [ADDEDBYID], [CHANGEDBYID], [DATEADDED], [DATECHANGED], [CATEGORYCODE], [SUBCATEGORYCODE] ) 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 [CONTRAACCOUNT], [CASHFLOWCODEID], [WORKINGCAPITALCODEID], [STATUSCODEID], [SITEID], [ADDEDBYID], [CHANGEDBYID], [DATEADDED], [DATECHANGED], [CATEGORYCODE], [SUBCATEGORYCODE] FROM DELETED INSERT INTO dbo.ACCOUNTCODEAUDIT( AUDITRECORDID, AUDITKEY, AUDITCHANGEAGENTID, AUDITDATE, AUDITTYPECODE, [CONTRAACCOUNT], [CASHFLOWCODEID], [WORKINGCAPITALCODEID], [STATUSCODEID], [SITEID], [ADDEDBYID], [CHANGEDBYID], [DATEADDED], [DATECHANGED], [CATEGORYCODE], [SUBCATEGORYCODE] ) 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 [CONTRAACCOUNT], [CASHFLOWCODEID], [WORKINGCAPITALCODEID], [STATUSCODEID], [SITEID], [ADDEDBYID], coalesce(@ChangeAgentID, [CHANGEDBYID]), [DATEADDED], coalesce(@DateChanged, [DATECHANGED]), [CATEGORYCODE], [SUBCATEGORYCODE] FROM INSERTED end |