Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Generating Primary Keys in SQL Server
Message
De
07/02/2001 07:18:10
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Conception bases de données
Divers
Thread ID:
00414202
Message ID:
00473362
Vues:
26
>As I recall, I set the Transaction Isolation Levl to Repeatable Read (I didn't care about new rows at this point). I then started a transaction and queried the PK table to find the last PK used. Once the row was read, it was locked so no one else could change it. I then updated the PK table using SET PK = @PK + 1 WHERE PK = @PK and commit the transaction. I then set the isolation level back to what it was.

Here's another way to do it:

This assumes that the column nextkey contains the next ID to use.

DECLARE @pk INTEGER
UPDATE keytable
SET @pk = nextkey, nextkey = nextkey + 1
WHERE table = something

This version will assumes that nextkey contains that last ID used. Which means that you have to increment it first. This is not a typo <s>

DECLARE @pk INTEGER
UPDATE keytable
SET @pk = nextkey = nextkey + 1
WHERE table = something

-Mike
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform