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 December 6th, 2006, 11:31 AM
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. The code looks logical to me, so hopefully it works. Thanks! :)

 
Old December 6th, 2006, 02:08 PM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello
the column to which u want to assign autogenerate number set IDENTITY Property as follows:

CREATE TABLE dbo.tablethree
    (
    /*
    [ID]is the Column Name,
        Int is its Datatype of an Integer,
        As this is our Identity Column we do not want to allow null values,
        IDENTITY (1,1) Sets the Column as Identity, (1 represents the very first seed to start with
        ,1) is the amount to increment by
    */
    [ID] int NOT NULL IDENTITY (1, 1),

    --here you write another colum declaration

it will work fine and will generate the auto values

thanks......
 
Old March 24th, 2007, 11:41 AM
Authorized User
 
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Jeff Moden
 ...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.&nbsp;&nbsp;The first number is A000 and the rollover numbers are B000, C000, D000, etc.&nbsp;&nbsp;It would be a fair bit more difficult to generate rollover numbers that were B001, C001, D001, etc.
 
Old March 24th, 2007, 11:47 AM
Authorized User
 
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

[quote]quote:Originally posted by okboy
 
Quote:
Originally posted by Jeff Moden
Quote:
 ...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.&nbsp;&nbsp;The first number is A000 and the rollover numbers are B000, C000, D000, etc.&nbsp;&nbsp;It would be a fair bit more difficult to generate rollover numbers that were B001, C001, D001, etc.

Jeff Moden, if i wanna let AlphaID to be primary key then how to do? example: A000, A0001 to be primary key.

 
Old March 24th, 2007, 03:12 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What happens after "Z9999"?


 
Old March 29th, 2007, 06:34 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
quote:Originally posted by Peso
 What happens after "Z9999"?
Boooommmmm! That's why I hate these types of numbering systems... they're pretty well finite.

--Jeff Moden





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 03:45 PM
SQL Server sync with net server and browser server ne SQL Server DTS 0 June 13th, 2005 06:29 PM





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