Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Generating Primary Keys in SQL Server
Message
From
07/02/2001 07:18:10
 
General information
Forum:
Microsoft SQL Server
Category:
Database design
Miscellaneous
Thread ID:
00414202
Message ID:
00473362
Views:
25
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform