October 20, 2005 10:20 PM

Should I Use IDENTITY or Not?

Sometimes Another Approach Works Better
Rating: (0)
SQL Server Magazine
InstantDoc ID #48165
At first glance, it seems there's nothing special to consider before you use the IDENTITY column property, SQL Server's auto-numbering mechanism. But especially in large environments, you'll first want to understand some of IDENTITY's workings and limitations. In some cases, you might prefer to use your own auto-numbering or sequencing mechanism.To help you decide on the best course, I provide some facts about IDENTITY, discuss some of its limitations, and show two alternatives.

...This article is for paid Professional Members only.

Already a Professional Member? Please log in now:

NOT A PROFESSIONAL MEMBER? YOU CHOOSE:

Professional Membership

Monthly

Annual

VIP Membership

Monthly

Annual

Add a Comment

Smack! You are good.

Patrick8/24/2008 2:59:31 AM


I would suggest a minor optimization.
In many cases we know up front that more than one value is necessary.
For instance if I am going to insert 15 new rows, I know in advance that I need 15 values.
It would be nice to do it in just one stored procedure call.
Let's modify the procedure a little bit:

CREATE PROC usp_newseq_interval @amount int, @v
AS INT OUTPUT
AS UPDATE Seq
-- allocates @amount iunique values
SET @v = val = val + @amount;
go

and call it:

DECLARE @i AS INT;
EXEC usp_newseq_interval 15, @v = @i OUTPUT;
SELECT @i;

As a result of this call, 15 consecutive values have been allocated,
and @i stores the last one of them.

Alexander2/10/2006 12:54:41 PM


I'm not sure if this is the proper place to post this but I have a question. I was recently working on an SP that would do the same thing, but I used this syntax:

UPDATE [TABLE]
SET @Val = [Column] + 1,
[Column] = [Column] + 1

would this do the same thing as mentioned in the article:

DECLARE @i AS INT;
UPDATE Seq SET @i = val = val +1;
SELECT @i;

Nick12/29/2005 8:14:33 AM


Another brilliant article (ideas) from Itzik. Such a simple solution to a complex problem, or at least, a problem most of us have spent time on, I just wish i'd thought of these solutions.
I hate u Itzik -lol- :)

Peter12/13/2005 10:59:25 PM


You must log on before posting a comment.

Are you a new visitor? Register Here
GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS