SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mssql - @@identity question

    What is the best way to get the id of the previously inserted row? For example, if User A causes an connection execution that runs an INSERT statement, then after that runs a SELECT @@IDENTITY to get the new row's id, then does a select on said row, it should work fine. But what happens if after you execute the first INSERT statement, User B comes in and does another INSERT on another table before User A's page reaches the SELECT @@IDENTITY? User A will get the wrong row, correct?

    What are the chances of this happening? Is there any way to avoid it?

    Thank you,
    Joe Fiorini
    MDDA- Panasonic
    -Joe

  2. #2
    \m/ R.I.P. Dimebag! \m/ JimBolla's Avatar
    Join Date
    Dec 2001
    Location
    erie, pa
    Posts
    1,130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually that won't happen because @@IDENTITY is per connection. User A could insert a row at 10:00 AM and user B could then insert hundreds of rows at 10:02 AM and user A could A could then call @@IDENTITY at 10:04 AM and still get the value he would be expecting.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that was kinda nice of microsoft. Thanks for the help!
    -Joe

  4. #4
    \m/ R.I.P. Dimebag! \m/ JimBolla's Avatar
    Join Date
    Dec 2001
    Location
    erie, pa
    Posts
    1,130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •