SQL Server UPDATE Trigger returning both updated and previous value

z8dt9xmd  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(83)

I am trying to create a trigger that will insert a record from a table into a another table whenever a record is updated.

CREATE TRIGGER [dbo].[trg_tblContent_backup_update] 
   ON [dbo].[tblContent]
   FOR UPDATE
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

     INSERT INTO tblContentUpdateBackup 
     (
        ContentID,
        revision,
        CountryID,
        QualificationID,
        ContentTitle,
        CourseID,
        ContentOrder,
        ContentStatus,
        ContentTypeID,
        Content,
        GenericPageID,
        GenericDisplay,
        altLanguage,
        locationID,
        languageID,
        defaultLanguage,
        courseGenericID,
        moduleID,
        ThemePageID,
        contentRelateID,
        alantestID,
        ContentBackup,
        LastModified,
        ProgrammeID,
        temp_contentRelateID
     
     )
     SELECT 
     
        ContentID,
        revision,
        CountryID,
        QualificationID,
        ContentTitle,
        CourseID,
        ContentOrder,
        ContentStatus,
        ContentTypeID,
        Content,
        GenericPageID,
        GenericDisplay,
        altLanguage,
        locationID,
        languageID,
        defaultLanguage,
        courseGenericID,
        moduleID,
        ThemePageID,
        contentRelateID,
        alantestID,
        ContentBackup,
        CURRENT_TIMESTAMP,
        ProgrammeID,
        temp_contentRelateID
     
     
     FROM INSERTED
    -- Insert statements for trigger here

END
GO

The trigger above that I have built inserts both the previous and updated records from tblContent into tblContentUpdateBackup, however I only want the previous record.

I'm nearly there, any help would be appreciated.

Thanks

cfh9epnr

cfh9epnr1#

A freelance developer who works for us came up with the following solution, which seems to do the job:

Hi @alancoleman

So I've spent a couple of hours on this since we chatted and after various tests and reading up the only thing I've find that looks like its working is to use TRIGGER_NESTLEVEL() condition ( found something on it here https://clivesyabb.com/2014/08/06/avoiding-recursive-triggers-in-sql-vs-avoiding-nested-execution/ )

When I was testing I could see that the trigger was being called twice ( for reasons I haven't worked out ) so by using

IF (TRIGGER_NESTLEVEL()) = 1

before we do any inserting on the backup table we can make sure that it hasn't been triggered before.

I've updated the trg_tblContent_backup_update trigger if you want to take a look - seemed to be working when I tested!

7gcisfzg

7gcisfzg2#

The above didn't work for me, but adding WHERE concatenation of the old != a concatenation of the new stopped the current value from writing to a history table (using insert and deleted tables). I too experienced the after update trigger writing the old and the new to history. This only happened with the app, not when testing CRUD operations in SQL SERVER.

相关问题