Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Auto-Numbering
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Auto-Numbering

 Post Reply Post Reply Page  123>
Author
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Topic: Auto-Numbering
    Posted: 26 June 2003 at 7:35pm

I'm trying to create an auto-numbering column (let's name it ID) in an SQL2000 table using the IDENTITY function. (Maybe they should make this a built-in function just like MS Access. Bummer! )

Everytime I click the Save button in my form, the data will be saved in the table and the ID column will be increased by 1. And so on. The problem is... I have no idea how to write the code that will do just that.

Am I doing the right thing here, or is there an alternative to this?

Back to Top
Gary View Drop Down
Senior Member
Senior Member
Avatar

Joined: 20 March 2002
Location: United Kingdom
Status: Offline
Points: 326
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gary Quote  Post ReplyReply Direct Link To This Post Posted: 27 June 2003 at 2:56am

Lets say you have an column, Ref, which contains the number. What you need to do is get the highest (Max) number and add one to it so as to get the next number....

strSQL = "SELECT MAX(Ref) AS LastRef FROM [myTable]"
SET rsSQL = con.execute(strSQL)
NextRef = rsSQL(LastRef) + 1

Not sure if that is what you are after.

Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 27 June 2003 at 8:05am
If the database field is set to be an IDENTITY column with the autoincrement of 1, it will automatically increment that field by 1 when a record is inserted.
Back to Top
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Posted: 30 June 2003 at 7:53pm

Originally posted by ljamal ljamal wrote:

If the database field is set to be an IDENTITY column with the autoincrement of 1, it will automatically increment that field by 1 when a record is inserted.

Yup.. that's what it says in the online book. Somehow I can't get it to work. So I tried another way. Thanks anyway.

Back to Top
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Posted: 30 June 2003 at 7:55pm
Originally posted by Gary Gary wrote:

Lets say you have an column, Ref, which contains the number. What you need to do is get the highest (Max) number and add one to it so as to get the next number....

strSQL = "SELECT MAX(Ref) AS LastRef FROM [myTable]"
SET rsSQL = con.execute(strSQL)
NextRef = rsSQL(LastRef) + 1

Not sure if that is what you are after.

Yup.. that's exactly what I'm after. I counted the number of rows, though.. since the table was initially blank. Thanks Gary.

Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 30 June 2003 at 8:44pm
Originally posted by Awangku Awangku wrote:

Yup.. that's what it says in the online book. Somehow I can't get it to work. So I tried another way. Thanks anyway.


That's the way it should work, if it's not working that way then something is wrong with your SQL Server.
Back to Top
Bunce View Drop Down
Senior Member
Senior Member
Avatar

Joined: 10 April 2002
Location: Australia
Status: Offline
Points: 846
Post Options Post Options   Thanks (0) Thanks(0)   Quote Bunce Quote  Post ReplyReply Direct Link To This Post Posted: 30 June 2003 at 9:54pm

You should not be using the method listed above (MAX) to create an 'auto-number' for your purposes.

What if another record was added in between you getting the maximum number and creating another?

Always use the functionality that the database provides.

IDENTITY will work for you, but you must have an incorrect setting somewhere.  Show us the relevant code you are using alloing with the database settings for that field.

Cheers,
Andrew

There have been many, many posts made throughout the world...
This was one of them.
Back to Top
Awangku View Drop Down
Groupie
Groupie


Joined: 15 November 2001
Location: Malaysia
Status: Offline
Points: 70
Post Options Post Options   Thanks (0) Thanks(0)   Quote Awangku Quote  Post ReplyReply Direct Link To This Post Posted: 02 July 2003 at 6:46pm

Well.. to be honest, identity doesn't work for me mainly because I don't know how to use it. I set a column as identity and when I tried to insert a new record into the table, I got an error message instead. Nothing wrong with my SQL server. It's just that I have no idea how to use it. Anyone here kind enough to teach me?

As for inserting new records into my table, well.. the database is set so that the ONLY way to modify it is through my asp interface. Heck, once this whole thing goes live, I can't even touch it. And I'm the developer!

So to get that (misleading) 'auto-number' thing, I just counted the number of rows currently in the table, add 1 and use that number when I add a new row.

 



Edited by Awangku
Back to Top
 Post Reply Post Reply Page  123>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.