Comment récuperer la dernière clé autoincrémenté en SQL Server 2000
Comment récuperer la dernière clé autoincrémenté en SQL Server 2000
Si mes souvenirs sont bons :
@+
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 SELECT @@identity
Comment savoir que l'autoincrément correspond à telle ou telle table ?
Quelle est la table associé à l'autoincrément ?
Je ne crois pas que tu puisses savoir de quelle table il provient.
Pour info, voilà les infos SQL-Server originales concernant @@identity :
@+@@IDENTITY
Returns the last-inserted identity value.
Syntax
@@IDENTITY
Return Types
numeric
Remarks
After an INSERT, SELECT INTO, or bulk copy statement completes, @@IDENTITY contains the last identity value generated by the statement. If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL. If multiple rows are inserted, generating multiple identity values, @@IDENTITY returns the last identity value generated. If the statement fires one or more triggers that perform inserts that generate identity values, calling @@IDENTITY immediately after the statement returns the last identity value generated by the triggers. The @@IDENTITY value does not revert to a previous setting if the INSERT or SELECT INTO statement or bulk copy fails, or if the transaction is rolled back.
@@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. For more information, see IDENT_CURRENT.
Examples
This example inserts a row into a table with an identity column and uses @@IDENTITY to display the identity value used in the new row.
INSERT INTO jobs (job_desc,min_lvl,max_lvl)
VALUES ('Accountant',12,125)
SELECT @@IDENTITY AS 'Identity'
j'ai compris.
Il est donc important de recuperer ton @@Identity dans une procedure stockee et à la suite d'un Insert et là vous êtes rassuerer de connaître la table concernée par la dernière insertion.
Bonjour,
Sauf si tu fais un insert dans la table1 qui par trigger fait un insert dans la table2 dans ce cas ton @@identity se rapportera à celui de table2 je crois.Il est donc important de recuperer ton @@Identity dans une procedure stockee et à la suite d'un Insert et là vous êtes rassuerer de connaître la table concernée par la dernière insertion.
A+
Bien entendu, vous devez vous rassurez que votre insert est la plus ressente.
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager