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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:49:33+00:00 2026-05-22T20:49:33+00:00

This seemed so easy at first but everything I know seems to be wrong

  • 0

This seemed so easy at first but everything I know seems to be wrong again.

Looking at PAQ’s the consensus seems to be that EXEC does not start an implicit transaction, you can test this doing :

create procedure usp_foo
as
begin
  select @@trancount;
end
go

exec usp_foo;

which returns 0.

If you however step through this with the T-SQL debugger @@Transaction is in fact 1 inside the procedure according to the watch, although it does return 0 …

So I think this is a side-effect of the debugger, but then I write some code to test it, do an update in a table and then select max (id) out the, the classic :

create table nextid
(
  id int
)

insert into nextid values (0)

create procedure nextid
as
BEGIN
  UPDATE nextid set id = id + 1
  select max(id) from nextid
END

So I am expecting this to give out duplicate id’s as executed in parallel 2 updates can complete before the 2 selects fetching the last id and returning the same value, but try as I might hitting it from multiple machines I cannot make it break. When monitoring the locks and transactions on the machine it reports that the exec is happening in a transaction and importantly, all the statements inside the exec are treated as one unit of work/one transaction.

I would understand if the update was in a transaction and this was the cause of the lock, but the lock seems to remain until after the select.

If I trace with profiler I can see that a transaction ID is provided for the entire execution of the EXEC statement, and transaction ID is not 0 as I would expect while executing …

Can someone please explain to me where I am missing the plot or am I wrong and it is in fact safe to generate ID’s like that?

  • 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-22T20:49:34+00:00Added an answer on May 22, 2026 at 8:49 pm

    Your test must be giving you correct results because you are not fast enough to invoke the second call in between these two statements. Try adding a delay and you can see that the test would start failing.

    CREATE TABLE NextID
    (
        ID int
    )
    GO
    
    INSERT INTO NextID VALUES (0)
    GO
    
    CREATE PROC GetNextID
    AS
    BEGIN
        UPDATE NextID SET ID = ID + 1
        WAITFOR DELAY '00:00:05'
        SELECT Max(ID) FROM NextID
    END
    

    Issue an EXEC GetNextID and issue another EXEC GetNextID as soon as you can from another session. About 5 seconds later both EXEC would return same result i.e. incorrect value. Now change the SP to

    CREATE PROC GetNextID
    AS
    BEGIN
        BEGIN TRAN
    
        UPDATE NextID SET ID = ID + 1
        WAITFOR DELAY '00:00:05'
        SELECT Max(ID) FROM NextID
    
        COMMIT TRAN
    END
    

    and repeat the above test. You would see that both calls would return correct value. In addition second call (if issued as soon as possible) would return result approximately in 10 seconds because the UPDATE is blocked and has to wait 5 seconds (for the first call to COMMIT) and then its own 5 second wait.

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

Sidebar

Related Questions

This seemed like it should be easy, but I have had trouble getting it
This one has me puzzled. It seemed like an easy task, but the solution
First off, I'm aware that there are many questions related to this, but none
I'm stuck at something that seemed easy but became a headache pretty fast: Here
This seemed to me like an easy one, but I can't seem to find
This seemed like an easy thing to do. I just wanted to pop up
In linking a sports event to two teams, at first this seemed to make
This question might belong on one of the other trilogies, but it sorta seemed
I am sure there will be articles around this but I really don't know
This seemed to spark a bit of conversation on another question and I thought

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.