SQL @@identity teruggeven multiusers

Pagina: 1
Acties:

  • dominion99
  • Registratie: December 2001
  • Laatst online: 13-08-2025
Ik heb zelf een functie in elkaar gezet die met een insert statement het laatst aangemaakte id teruggeeft in asp.net.

Nu is mijn vraag, kan het nu voorkomen dat als er meerder users tegelijk bezig zijn dat de @@identity een verkeerde waarde teruggeeft.

Het gaat hier om de volgende insert op een SQL 2000 server
code:
1
"insert into klantex (relatienr) values ('" & relatieid & "');" & "select @@identity as id"


Dus als 2 personen alletwee dit insert statement tegelijk laat uitvoeren dat je dan de ID krijgt van de laatst ingevoerde record?

Ik vraag me dit nl af omdat ik zo'n voorbeeld nog nergens ben tegengekomen, daarom twijfel ik of het wel goed is.

  • dominic
  • Registratie: Juli 2000
  • Laatst online: 21-08 19:07

dominic

will code for food

Handig om dan met transactions te gaan werken. Die locked die zooi ffies op het moment dat de query uitgevoerd wordt.

Download my music on SoundCloud


  • pasz
  • Registratie: Februari 2000
  • Laatst online: 16-08 23:04
als het goed is regelt sql server dat, en geeftie het laatste id terug van de huidige sessie

woei!


  • dominion99
  • Registratie: December 2001
  • Laatst online: 13-08-2025
hoe werkt dat transaction ongeveer? kun je dan een table locken of zo

  • Crazy D
  • Registratie: Augustus 2000
  • Laatst online: 14-08 12:38

Crazy D

I think we should take a look.

@@identity geeft het nummer terug wat jij met die query vanaf die client in die thread zojuist hebt gebruikt.. zo vang je wel ongeveer alles af ;). Oftewel, je krijgt gewoon het juiste id terug.
Zou ook een beetje raar zijn als dat anders zou zijn, anders heb je nl niks aan @@identity en kun je net zo goed zelf een max(id) query doen.

Exact expert nodig?


  • dominion99
  • Registratie: December 2001
  • Laatst online: 13-08-2025
Nou ik heb toch nog ergens gelezen dat je met Max(id) toch niet altijd het gewenste resultaat krijgt

  • pasz
  • Registratie: Februari 2000
  • Laatst online: 16-08 23:04
Transactions kan op twee manieren. Of je zet de hele zooi in een stored procedure, of je slingert dit aan vanuit bijv. ASP/Visual Basic... ik weet niet wat je gebruikt

woei!


Verwijderd

met max(id) niet idd maar @@identity geeft het recordid terug van het laatst geinserte record op de huidige database verbinding, andere gebruiker heeft andere verbinding en krijgt dus ook andere @identity's terug dus gewoon blijven gebruiken, nix mis mee

  • dominion99
  • Registratie: December 2001
  • Laatst online: 13-08-2025
Ok dan houden we het wel zo als dat het is, btw ik schrijf het in asp.net en VB.Net

  • Annie
  • Registratie: Juni 1999
  • Laatst online: 25-11-2021

Annie

amateur megalomaan

Ik gebruik voor SQL2000 liever SCOPE_IDENTITY()
(zie de BOL voor meer info).

Today's subliminal thought is:


  • Crazy D
  • Registratie: Augustus 2000
  • Laatst online: 14-08 12:38

Crazy D

I think we should take a look.

Die is idd wel handig ja!
SCOPE_IDENTITY and @@IDENTITY will return last identity values generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.

For example, you have two tables, T1 and T2, and an INSERT trigger defined on T1. When a row is inserted to T1, the trigger fires and inserts a row in T2. This scenario illustrates two scopes: the insert on T1, and the insert on T2 as a result of the trigger.

Assuming that both T1 and T2 have IDENTITY columns, @@IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on T1.

@@IDENTITY will return the last IDENTITY column value inserted across any scope in the current session, which is the value inserted in T2.

SCOPE_IDENTITY() will return the IDENTITY value inserted in T1, which was the last INSERT that occurred in the same scope. The SCOPE_IDENTITY() function will return the NULL value if the function is invoked before any insert statements into an identity column occur in the scope.

Exact expert nodig?


  • robjanssen
  • Registratie: September 2001
  • Laatst online: 02-08 16:10

robjanssen

Software Developer

@@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT are similar functions in that they return the last value inserted into the IDENTITY column of a table.

@@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.

IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.

Verwijderd

SCOPE_IDENTITY gebruiken. @@IDENTITY geeft fouten wanneer je dmv een insert een trigger triggert die in een andere table weer een insert doet. SCOPE_IDENTITY werkt niet op SQLServer 7.
Pagina: 1