Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 923831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:15:14+00:00 2026-05-15T19:15:14+00:00

Here is one very interesting problem. I am using SQL Server 2008. I have

  • 0

Here is one very interesting problem. I am using SQL Server 2008.
I have two triggers on one common table say ‘CommonTable’. one trigger is on update and other one is on insert/update/delete.

  • In first trigger “Trigger1”, I do the checks/rollback sometime change the new inserted value based on business logic.
    here is sample code

–

CREATE TRIGGER [dbo].[Trigger1] ON [dbo].[CommonTable]
FOR UPDATE
UPDATE [CommonTable] 
SET 
    [StatusCode] = 'New Value'
WHERE 
[RecId] = 'rec id value'
  • In second trigger “Trigger2”, I store the new inserted/deleted/updated value from ‘CommonTable’ table to another table ‘CommonTable_History’ for history tracking purpose.
    here is sample code

–

CREATE TRIGGER [dbo].[Trigger2] ON [dbo].[CommonTable]
FOR INSERT, UPDATE, DELETE

--based on logic read the value from DELETED or INSERTED table and store in other table.

SELECT @RowData = (SELECT * FROM DELETED AS [CommonTable] WHERE [RecId] = @RowRecId FOR XML AUTO,                       BINARY BASE64 , ELEMENTS)

--and then insert @RowData in 'CommonTable_History' table.

With the help of ‘sp_settriggerorder’, I have set the order of execution of these triggers, so first “Trigger1” get executed and then “Trigger2”.

Second trigger “Trigger2” works well for insert/delete values. It works fine for new inserted value if new inserted values has not been changed by first trigger “Trigger1”.

But if in some cases, inserted values has been changed in “Trigger1”. say [StatusCode] = ‘New Value’ and old values was ‘Old Value’ then “Trigger2” still store the ‘Old Value’ instead of ‘New Value’.
Why because “Trigger1” change the value but that value still has not been store in database and before that “Trigger2” get executed on Insert.
Now my requirement is, here I want to store “New Value”.

So I thought, lets make “Trigger2” to use “AFTER” keywords. But “FOR” and “AFTER” behave same could not solve the problem.

Then I thought, lets make “Trigger2” to use “INSTEAD OF” keyword. But “INSTEAD OF” gives following error
“Cannot CREATE INSTEAD OF DELETE or INSTEAD OF UPDATE TRIGGER. This is because the table has a FOREIGN KEY with cascading DELETE or UPDATE.”

I can not remove FOREIGN KEY with cascading DELETE or UPDATE for table ‘CommonTable’.

Please let me know if you people have any other alternate solution.
-Vikram Gehlot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:15:15+00:00Added an answer on May 15, 2026 at 7:15 pm

    I think your second trigger needs to use the values from the actual table, not the inserted/deleted tables to populate the log table – inserted/deleted will always have the unaltered, original values, while your altered values will appear in the table. Make the second trigger an “After” trigger, so you will not have to use the sp_settriggerorder. Like this, for example:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TRIGGER [dbo].[trg_Trig1] 
       ON  [dbo].[TestTable] 
       FOR INSERT
    AS 
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        update TestTable
        set [value] = 10
        where [value] = 25
    
    END
    
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TRIGGER [dbo].[trg_Trig2]
       ON  [dbo].[TestTable] 
       AFTER INSERT
    AS 
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Insert statements for trigger here
        insert into log_TestTable
        (id, description, [value])
        select tt.id, tt.description, tt.[value]
        from inserted i
            LEFT JOIN TestTable tt
                ON tt.id = i.id
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How else would Spring find the classes? If you haven't… May 16, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer You might want to look at System.Environment.GetFolderPath. The values of… May 16, 2026 at 3:52 pm
  • Editorial Team
    Editorial Team added an answer What I have found to be the main culprit with… May 16, 2026 at 3:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a problem very similar to this one : CATransaction Not Animating I'm
there's this interesting problem i can not solve myself. I will be very glad,
I have read several interesting C#/F# comparisons here on stackoverflow. So, first of all,
I'm running into an interesting problem in Powershell, and haven't been able to find
I ran into an interesting (and very frustrating) issue with the equals() method today
this post is a long one sorry for that but the problem is complex
I got a weird problem. I have a program that (does many things but
I have a C# application that allows one user to enter information about customers
Sorry about the title. I do realize it's not very descriptive. :| Here is
tl;dr = Anyone know how to apply chained classes for IE6 using jQuery or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.