Wrox Programmer Forums
|
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
 
Old February 8th, 2005, 06:26 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default AutoNumber Assignments

hi
     Is there a way to tell which number is going to be assigned next in an auonumber field in SQL?

 
Old February 8th, 2005, 06:39 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT MAX(AutoIDField) As NextID From Table



 
Old February 8th, 2005, 06:49 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you shahchi1

 
Old February 8th, 2005, 06:52 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This only seems to return the last record id in the table not the next autonumber field to be assigned

 
Old February 8th, 2005, 07:03 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

SELECT MAX(isnull(AutoIDField,0)) + 1 As NextID From Table

Om Prakash
 
Old February 8th, 2005, 07:18 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 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.

 
Old February 8th, 2005, 08:47 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

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
 
Old February 9th, 2005, 12:58 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

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
 
Old February 9th, 2005, 02:10 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx madhukp
     this is exactly what i was getting at

 
Old February 9th, 2005, 02:51 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Autonumber Query Brendan Bartley Access 2 July 16th, 2006 02:14 AM
AutoNumber causes problem. myself Classic ASP Professional 1 July 10th, 2006 03:35 PM
regarding Autonumber... MuthuAL Classic ASP Databases 2 December 8th, 2004 08:04 AM
Access Autonumber chall90909 Access ASP 1 November 21st, 2004 10:26 AM
Retrieving the AutoNumber stu9820 ASP.NET 1.0 and 1.1 Basics 11 August 18th, 2004 04:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.