Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 8th, 2005, 06:26 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
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?

Reply With Quote
  #2 (permalink)  
Old February 8th, 2005, 06:39 PM
Friend of Wrox
 
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT MAX(AutoIDField) As NextID From Table



Reply With Quote
  #3 (permalink)  
Old February 8th, 2005, 06:49 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you shahchi1

Reply With Quote
  #4 (permalink)  
Old February 8th, 2005, 06:52 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
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

Reply With Quote
  #5 (permalink)  
Old February 8th, 2005, 07:03 PM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

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

Om Prakash
Reply With Quote
  #6 (permalink)  
Old February 8th, 2005, 07:18 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
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.

Reply With Quote
  #7 (permalink)  
Old February 8th, 2005, 08:47 PM
Friend of Wrox
Points: 2,473, Level: 20
Points: 2,473, Level: 20 Points: 2,473, Level: 20 Points: 2,473, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: India
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
Reply With Quote
  #8 (permalink)  
Old February 9th, 2005, 12:58 AM
Friend of Wrox
 
Join Date: Oct 2003
Location: Cochin, Kerala, India.
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
Reply With Quote
  #9 (permalink)  
Old February 9th, 2005, 02:10 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx madhukp
     this is exactly what i was getting at

Reply With Quote
  #10 (permalink)  
Old February 9th, 2005, 02:51 PM
Registered User
 
Join Date: Feb 2005
Location: , , .
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:44 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.