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 6683471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:49:32+00:00 2026-05-26T04:49:32+00:00

We have a trigger that creates audit records for a table and joins the

  • 0

We have a trigger that creates audit records for a table and joins the inserted and deleted tables to see if any columns have changed. The join has been working well for small sets, but now I’m updating about 1 million rows and it doesn’t finish in days. I tried updating a select number of rows with different orders of magnitude and it’s obvious this is exponential, which would make sense if the inserted/deleted tables are being scanned to do the join.

I tried creating an index but get the error:
Cannot find the object "inserted" because it does not exist or you do not have permissions.

Is there any way to make this any faster?

  • 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-26T04:49:32+00:00Added an answer on May 26, 2026 at 4:49 am

    Inserting into temporary tables indexed on the joining columns could well improve things as inserted and deleted are not indexed.

    You can check @@ROWCOUNT inside the trigger so you only perform this logic above some threshold number of rows though on SQL Server 2008 this might overstate the number somewhat if the trigger was fired as the result of a MERGE statement (It will return the total number of rows affected by all MERGE actions not just the one relevant to that specific trigger).

    In that case you can just do something like SELECT @NumRows = COUNT(*) FROM (SELECT TOP 10 * FROM INSERTED) T to see if the threshold is met.

    Addition

    One other possibility you could experiment with is simply bypassing the trigger for these large updates. You could use SET CONTEXT_INFO to set a flag and check the value of this inside the trigger. You could then use OUTPUT inserted.*, deleted.* to get the “before” and “after” values for a row without needing to JOIN at all.

    DECLARE @TriggerFlag varbinary(128)
    SET @TriggerFlag = CAST('Disabled' AS varbinary(128)) 
    
    SET CONTEXT_INFO @TriggerFlag
    
    UPDATE YourTable
    SET Bar = 'X'
    OUTPUT inserted.*, deleted.* INTO @T
    
    /*Reset the flag*/
    SET CONTEXT_INFO 0x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL script that acquires table names and creates trigger for those
I have a sql script that creates several tables, a trigger, and a trigger
We have created a table with a trigger that updates a ModifiedDate field in
I have a trigger that stores changes made in a separate table when a
I have a table in a SQL Server 2005 database with a trigger that
I have a table that has an insert trigger on it. If I insert
I have a number of tables that use the trigger/sequence column to simulate auto_increment
I have a trigger in SQL Server 2005 that is used to track audit
I have a table that has a Trigger associated with it for Update, Insert
I have a trigger that sets a datetime field in a table row when

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.