 |
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

February 8th, 2005, 06:26 PM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
AutoNumber Assignments
hi
Is there a way to tell which number is going to be assigned next in an auonumber field in SQL?
|

February 8th, 2005, 06:39 PM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SELECT MAX(AutoIDField) As NextID From Table
|

February 8th, 2005, 06:49 PM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you shahchi1
|

February 8th, 2005, 06:52 PM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This only seems to return the last record id in the table not the next autonumber field to be assigned
|

February 8th, 2005, 07:03 PM
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
|
|
SELECT MAX(isnull(AutoIDField,0)) + 1 As NextID From Table
Om Prakash
|

February 8th, 2005, 07:18 PM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SELECT MAX(isnull(AutoIDField,0)) + 1 As NextID From Table
Finds the next nemeric cnumber in the list, but lets say i have 10 records.. id's 1 through 10 and delete the last 5. Then i have records 1 to five, but the next autonumber field is going to be 11 unless i compress the database which i do not want to do.
|

February 8th, 2005, 08:47 PM
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
|
|
SELECT MAX(isnull(AutoIDField,0)) + 1 As NextID From Table
--Yes: this will give the max + 1 record from the data. If u delete last 5, then next number is 6, and not 11.
Even if you read the next number as 11, then it is possible that by the time u display the data & add, somebody has populated the data.
You can display the autonumber after inserting the data. which will be 11, if your column is autonumber.
Om Prakash
|

February 9th, 2005, 12:58 AM
|
Friend of Wrox
|
|
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What Om Prakash told is correct. This method is not thread safe. Not correct also. If last record is deleted, you will get back the id of that record in this method whereas the actual id will be one more than that.
Could you please specify why you want to get that value ? If it is for inserting child records of a different table, there is another method. You can write a stored procedure to do the insertion into the parent table and get back the new id generated. This you can use to fill child record.
A sample stored procedure for this is given.
CREATE PROCEDURE sp_insert_record(@value1 varchar(50), @value2 INT, @id INT OUTPUT)
AS
INSERT INTO table(val1, val2) VALUES (@value1, @value2)
SELECT @id=@@IDENTITY
|

February 9th, 2005, 02:10 PM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanx madhukp
this is exactly what i was getting at
|

February 9th, 2005, 02:51 PM
|
Registered User
|
|
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
is there a way to autogenerate a number and insert it into 2 table inside a strored procedure? What i am trying to do is add a record to one table, and the autonumber field from the parent table will automatically go into the child table.
|
|
 |