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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:09:35+00:00 2026-06-06T19:09:35+00:00

This problem is about concurrent high speed inserts. I must admit it is very

  • 0

This problem is about concurrent high speed inserts.
I must admit it is very interesting in my eyes.

I’m using SQL Server 2008 R2 executing T-SQL inserts and after insert trigger.

I would like to make sure that no command will execute between an insert and its after insert trigger.

Using isolation levels causes deadlocks or just does NOT solve the problem.

The procedure I’m using is the answer/solution from Phil to
SQL Server dependent Identity – is there such a thing?

The problem is:

sometimes an insert get in between a previous insert and its after insert trigger, causing this result:

RoomID  ItemID  ItemDescription ID
------  ------  --------------- --
7       1       Door             1
7       2       Window (West)    2
7       3       Window (North)   3
8       1       Door             4
8       2       Table #1         5
8       3       Table #2         6
7       4       Table #1         7
8       4       Chair #1         8
7       6       Table #2         9
7       5       Table #3        10
8       5       Chair #2        11

See IDs #9 and #10. Thair ItemIDs are switched. The ItemID should have been 5 and 6 respectively and not 6 and 5, but the 10th insert probably occurred before the after insert trigger of #9 finished execution.

This problem happens less than 0.5% of the inserts: 2 switches involving 4 records for 1000 inserts or less. Yes, sometimes there are no switches occurrences.

Raising the isolation level by one step does not help, and even cause more keys/dependent-keys switches from time to time. Raising two isolation levels up caused deadlocks.

Lowering the isolation level reduces switches, but they are still created.

Starting raised isolation level before the every insert and moving back to the default isolation level at the end of the trigger causes deadlocks (in my experiments all inserts did not commit!).

Do anyone see a way out?

How to force an insert and its after insert trigger to execute together, prohibiting other inserts to the same table in between?

  • 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-06-06T19:09:37+00:00Added an answer on June 6, 2026 at 7:09 pm

    How about using an instead of insert trigger?

    Of course, you will have to make the actual insert yourself, but that’s the supported behavior in SQL Server. There are no before insert triggers or something similar.

    Anyway, you can still use your current trigger processing logic in this type of trigger as well.

    To try and imagine a better solution, can you explain what you you are trying to do? Are multiple threads inserting in this table? Is this a real-time process?

    It’s auction like. Yes, multiple threads are inserting in this table.
    The problem is rear but happens. No reasons for complaints yet, but
    there may be (the switches happened by now where not at the critical
    point). Users who initiate inserts need to know what is their
    precedence in a list of orders. The ItemID is used almost imminently,
    therefore, may not be calculated on the fly (with select count(*)…
    where..) later on when needed a matter of milliseconds. Wrong order of
    ItemID may cause losses to one and non rightful gains for another.

    Well, multithreaded inserts are almost never a good idea in SQL server. At least not without some sort of synchronization.

    You need some kind of queuing.
    For example, I have a similar system where need to insert between 2.5-3 million records per day (well, working hours).

    At first I also used 8-16 threads to do my inserts directly on the database. And I noticed exactly this behavior: deadlocks. So I start thinking about how I could queue these messages so only 1 thread (1 connection) will be inserting at any time.

    I ended up queuing these records in MSMQ (transactional). Some other process comes and takes them from here (also transactional) and inserts them in batches in the database. My records are guaranteed to come in the correct order, the one in which they were sent.

    So this secondary process inserts batches of records and, as you, I need some pre-processing before inserting, which I’m doing with an instead-of-insert trigger. There I can update the entire inserted table “quietly”, without worrying that someone can come and mess my stuff.

    It’s just an idea. You may also want to consider Service Broker for queuing, especially if you don’t want to take the processing outside SQL Server.

    Also, something worth trying are transactions with Snapshot Isolation level and row versioning, but I recommend going the MSMQ/Service Broker way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP experts, I've been working on this problem for about a day and a
Searched a lot about this problem but never found a answer, that solved it.
i find a lot of questions about this problem but i did not solve...
I read many posts about this problem like [this link][1] and one solution is
I've been wondering about this problem for some time - doubleclicking a winforms TabControl's
[Note: There is another thread about this problem but it did not answer the
I noticed some similar questions about this problem when I typed the title, but
I have tried searching over the internet about this problem but not able to
I know there is a lot of thread about this problem but I dont
I have an IIS server which serves a search page using SolrNet. The search

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.