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 Display Modes
  #1 (permalink)  
Old August 19th, 2003, 11:12 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: abu dbabi, , United Arab Emirates.
Posts: 518
Thanks: 0
Thanked 0 Times in 0 Posts
Default autonumber in sql server

There is autonumber datatype (option etc) available in sql server ?
I want one field which datatype autonumber, it
will automatically insert number when data insert on each record.
1, 2 , 3, 4,.... and so on
just like in ACCESS database there is autonumber datatype in sql server ?

Mateen
Reply With Quote
  #2 (permalink)  
Old August 19th, 2003, 11:33 PM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Canuck
Default

Make your field an "int", and then in the column attributes, make it an identity field (set it to "yes" - no quotes).

Reply With Quote
  #3 (permalink)  
Old August 20th, 2003, 03:05 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you have a lot of data to be inserted, then "int" might be filled up pretty quick. Perhaps a "Decimal" would be better?
Reply With Quote
  #4 (permalink)  
Old August 20th, 2003, 03:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You'd need rather a lot of data to exhaust an int (from bol):-

[u]bigint</u>

Integer (whole number) data from -2^63 (-9223372036854775808) through 2^63-1 (9223372036854775807).

[u]int</u>

Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647).



Using decimal is a waste of space (literally!) because it uses extra bytes to hold precision and scale information.††If you really are going to outgrow the basic int, use bigint instead.

HTH

Chris

There are two secrets to success in this world:
1. Never tell everything you know
Reply With Quote
  #5 (permalink)  
Old August 20th, 2003, 04:07 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Chris, you are of course correct. I wrote before I thought...
Reply With Quote
  #6 (permalink)  
Old August 20th, 2003, 09:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not only will the decimal (floating point) numbers require more storage space, they will also process slower (floating point math vs. integer math).

Rand
Reply With Quote
  #7 (permalink)  
Old August 20th, 2003, 10:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by rgerald
 Not only will the decimal (floating point) numbers require more storage space, they will also process slower (floating point math vs. integer math).
A decimal number requires 1 extra byte to store precision and scale information. The number of bytes required to represent a given value varies with the precision of the number itself and can vary from 5 to 17 bytes (including the 'overhead' byte). Thus, it may very well require less storage space than other representations.

Decimal numbers are also not floating point numbers - they are stored internally as scaled integers and are thus exact representations of a value, unlike floating point. Since they are scaled, though, arithmetic operations require software handling (to manipulate the decimal point), so they are indeed slower than integers, but you shouldn't be doing much with arithmetic operations in the database anyway...

BTW, have you measured the performance of the floating point processor on Intel machines these days? You may be surprised at how few cycles it takes for floating point math (admittedly still far slower than integer operations).

My biggest problem with the decimal datatype is the relatively poor client support for that type.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #8 (permalink)  
Old August 22nd, 2003, 01:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: abu dbabi, , United Arab Emirates.
Posts: 518
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks


Quote:
quote:Originally posted by Canuck
 Make your field an "int", and then in the column attributes, make it an identity field (set it to "yes" - no quotes).

Reply With Quote
  #9 (permalink)  
Old November 17th, 2006, 12:02 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Sorry, I have one problem on autogenerating ID too. I'm using Microsoft SQL and my IDs are in A001, A002, A003...and so on. How can I auto generate this IDs? Like A001 +1=A002? Please help...
Thanks
Lynn
Reply With Quote
  #10 (permalink)  
Old November 17th, 2006, 03:11 AM
Friend of Wrox
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 473
Thanks: 0
Thanked 7 Times in 7 Posts
Default

...And I assume that when you get to A999 and insert one more record, it should roll over to B000, huh?

Ok, in order to keep this from becoming "Death by SQL", here's how to do it... your table should have two column in it that are very similar to the ID and AlphaID columns... like this...

Code:
 CREATE TABLE AlphaID 
        (
        ID INT IDENTITY(0,1) PRIMARY KEY CLUSTERED ,
        AlphaID AS CHAR(ID/1000+65)+REPLACE(STR(RIGHT(ID,3),3),' ','0'),
        SomeOtherCol VARCHAR(20)
        )

††ALTER TABLE dbo.AlphaID 
    ADD CONSTRAINT CK_AlphaID_MaxID 
        CHECK (ID < 26999)


The key here is two things... the ID column and the calculated column called AlphaID.††Neat formula, huh?

And, to demo the table in the code above, let's do 10,000 quick little inserts and then select from the table... don't blink or you'll miss it...

Code:
 INSERT INTO AlphaID (SomeOtherCol)
†SELECT TOP 10000 'TestOnly'
†††FROM Master.dbo.SysColumns sc1,
        Master.dbo.SysColumns sc2

†SELECT * FROM AlphaID
--Jeff Moden

p.s. The first number is A000 and the rollover numbers are B000, C000, D000, etc. It would be a fair bit more difficult to generate rollover numbers that were B001, C001, D001, etc.
Reply With Quote
Reply


Thread Tools
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
vb.net sql autonumber insert GIGAM25 SQL Language 1 July 10th, 2008 01:10 AM
Conflict in SQL Server 2000 and SQL Server 2005 ayan.mukherjee SQL Language 0 June 30th, 2008 03:34 AM
SQL Server Reg. SQL Server does not exist error Arsi SQL Server 2000 1 June 11th, 2008 11:20 AM
migrating from sql server 2000 to sql server 2005 abinashpatra SQL Server 2005 2 December 1st, 2006 02:45 PM
SQL Server sync with net server and browser server ne SQL Server DTS 0 June 13th, 2005 06:29 PM



All times are GMT -4. The time now is 08:33 PM.


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