|
-
May 13th, 2004, 10:36 AM
#1
Thread Starter
Frenzied Member
get next ID number
In SQL Server 2000, with an autonumber ID field, how do you get the value that will be the ID of the next record (without setting it)?
-
May 13th, 2004, 10:46 AM
#2
SELECT TOP(autonumberidfield) FROM tablename
and add 1 to it!
But keep in mind, if another user INSERTs before you do, you won't get the right number.
-
May 13th, 2004, 10:48 AM
#3
Fanatic Member
Use the IDENT_CURRENT() function:
Code:
Select IDENT_CURRENT('table_name') + 1
IDENT_CURRENT returns the last identity value added to a table. Incrementing that number by one should return the next number.
Chris
Master Of My Domain
Got A Question? Look Here First
-
May 13th, 2004, 10:52 AM
#4
Thread Starter
Frenzied Member
Originally posted by mendhak
But keep in mind, if another user INSERTs before you do, you won't get the right number.
Ok, thanks guys... and that won't be a problem... it's more for display than anything else. They actual update will grab the true value and it isnt' necessary for them to know the value anyways.
-
May 13th, 2004, 12:24 PM
#5
Thread Starter
Frenzied Member
Ok, neither of those work and I just realized that I'm still using my test server (SQL Server 7). I haven't migrated to the SQL 2000 Server yet.
Anyone know how to do it with Server7?
-
May 13th, 2004, 02:02 PM
#6
try
SELECT ISNULL(MAX(autonumberidfield),0)+1 AS NextId FROM tablename
-
May 14th, 2004, 07:17 AM
#7
Thread Starter
Frenzied Member
Nope, that doesn't work either. That grabs the next one in sequence, but there have been several deleted and thus the number it should be grabbing should be higher than that. (I'm getting 737 and it should be more like 750 something)
-
May 14th, 2004, 12:24 PM
#8
Thread Starter
Frenzied Member
-
May 14th, 2004, 09:52 PM
#9
Hyperactive Member
Not near a 100% solution, since I've no v7 to test or validate this on. But I hope it helps or
gives you an idea or two.
Code:
-- As vb_dba says in mssql2k+: SELECT IDENT_CURRENT('tablename') + IDENT_INCR('tablename')
SELECT name
FROM sysobjects
WHERE type = 'U' AND IDENT_SEED(name) IS NOT NULL
SELECT
IDENT_SEED(OBJECT_NAME(id)) AS Seed
, IDENT_INCR(OBJECT_NAME(id)) AS Increment
, OBJECT_NAME(id)
, autoval
FROM syscolumns
WHERE (status & 128) = 128
-- see MSDN Q104829 for sp_hexadecimal
declare @binvalue varbinary(256), @hexvalue varchar(256)
SET @binvalue = (SELECT autoval FROM syscolumns WHERE name = 'IdentFieldName')
EXEC sp_hexadecimal @binvalue, @hexvalue OUTPUT
--print @hexvalue
-- curIdent___ Step_______ Seed_______
-- 0x FE 08 00 00 01 00 00 00 01 00 00 00 82
DECLARE @hexstr varchar(10), @curIdent int , @NextStep int , @cmdstr nvarchar( 255 )
SET @hexstr = '0x' + substring( @hexvalue, 9, 2 ) + substring( @hexvalue, 7, 2 ) + substring( @hexvalue, 5, 2 ) + substring( @hexvalue, 3, 2 )
SET @cmdstr = N'SELECT @curIdent = convert( int , ' + @hexstr + ' )'
EXEC sp_ExecuteSql @cmdstr, N'@curIdent int out', @curIdent out
--SELECT @curIdent
SET @hexstr = '0x' + substring( @hexvalue, 17, 2 ) + substring( @hexvalue, 15, 2 ) + substring( @hexvalue, 13, 2 ) + substring( @hexvalue, 11, 2 )
SET @cmdstr = N'SELECT @NextStep = convert( int , ' + @hexstr + ' )'
EXEC sp_ExecuteSql @cmdstr, N'@NextStep int out', @NextStep out
SELECT @curIdent + @NextStep NextIdentVal
Ah, the ways I love to not miss pre-2K days!
;-)
-
May 17th, 2004, 07:06 AM
#10
Thread Starter
Frenzied Member
wow... it's not THAT important. Thanks anyways.
-
May 18th, 2004, 11:36 AM
#11
New Member
Try looking at @@identity in books online, I'm sure that has something to do with it from memory.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|