December 2, 2010 at 9:12 pm
hi
I have a table that contains records that identified by a number. When a new record is inserted, I need to generate the next number in sequence. (was going to do 'select max(number) from....' however there are gaps in the number and so I need to find one of these gaps and use a number from there
eg there are 4 records in the table with the numbers 74,75,80,81. I need to add a new record with the number 76.
How do I find the number 76??:-)
December 2, 2010 at 10:02 pm
jane47 (12/2/2010)
hiI have a table that contains records that identified by a number. When a new record is inserted, I need to generate the next number in sequence. (was going to do 'select max(number) from....' however there are gaps in the number and so I need to find one of these gaps and use a number from there
eg there are 4 records in the table with the numbers 74,75,80,81. I need to add a new record with the number 76.
How do I find the number 76??:-)
I would say that's a bad idea. How would you handle the situation when you roll over into the existing records? (In this case, once you start off from 76, how would you handle the record with the number 80?)
Secondly, why would you want to resuse a number which has already been issued once?
Finally, if the column is an identity column, you will not be able to insert the same value again, provided IDENTITY_INSERT is OFF (default).
Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Follow me on
Twitter: @sqltwins
December 2, 2010 at 10:31 pm
I got this sweet little bit of delightful SQL code from MVP Jeff Moden:
DECLARE @test-2 TABLE (ID INT PRIMARY KEY CLUSTERED);
INSERT INTO @test-2
SELECT 74 UNION ALL
SELECT 75 UNION ALL
SELECT 80 UNION ALL
SELECT 81;
--===== Find the "gap ranges" --This takes 3 seconds on my 8 year old machine
-- Finds trailing edge of "islands" and then computes the gaps
-- This assumes that gaps include any whole number greater than 0
SELECT GapStart = ISNULL((SELECT MAX(lo.ID+1)
FROM @test-2 lo
WHERE lo.ID < hi.ID),1),
GapEnd = hi.ID - 1
FROM @test-2 hi
WHERE hi.ID NOT IN (SELECT ID + 1 FROM @test-2)
AND hi.ID > (SELECT MIN(ID) FROM @test-2);
--AND hi.ID > 1; -- if you consider it a gap if 1 should be present
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
December 2, 2010 at 10:55 pm
jane47 (12/2/2010)
hiI have a table that contains records that identified by a number. When a new record is inserted, I need to generate the next number in sequence. (was going to do 'select max(number) from....' however there are gaps in the number and so I need to find one of these gaps and use a number from there
eg there are 4 records in the table with the numbers 74,75,80,81. I need to add a new record with the number 76.
How do I find the number 76??:-)
This is always a problem and can be a source of deadlocks, as well. Considering how big INT and BIGINT actually are and how bullet-proof IDENTITY columns are, why do you need to use/reuse numbers that have gaps in them?
--Jeff Moden
Change is inevitable... Change for the better is not.
December 2, 2010 at 11:29 pm
I need to use the numbers because my customer initially blocks out a range of numbers, then adds records using these blocked numbers, or the next available number in the system (depends on the option selected) and then later on, any numbers not used in the range can be released and are then able to be used as the next available number in the system.
eg blocked range 40-50.
records are added for 40 to 45 also 51 and 52
numbers 46-50 are then released.
so the next number that should be used is 46 not 53. 53 would only be used once 46-50 have be used.
December 2, 2010 at 11:34 pm
I was planning to have a function that worked out if there was a gap in the numbers eg 76, then use that. I just posted an explanation of why I have gaps in my numbers.
I need to know how to find the next number whether its a gap or not
December 2, 2010 at 11:42 pm
thanks WayneS - just what I was looking for
December 3, 2010 at 5:18 am
jane47 (12/2/2010)
thanks WayneS - just what I was looking for
I'm glad it's working for you.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
December 3, 2010 at 8:34 pm
jane47 (12/2/2010)
I need to use the numbers because my customer initially blocks out a range of numbers, then adds records using these blocked numbers, or the next available number in the system (depends on the option selected) and then later on, any numbers not used in the range can be released and are then able to be used as the next available number in the system.eg blocked range 40-50.
records are added for 40 to 45 also 51 and 52
numbers 46-50 are then released.
so the next number that should be used is 46 not 53. 53 would only be used once 46-50 have be used.
Thanks, Jane. That explains "what" the customer wants but it doesn't explain "why" the customer wants to do it this way. Ah well... if you ever get the time and the customer doesn't mind participating, I'd sure like to know the business reasons for doing this type of thing. Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply