Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 5th, 2006, 01:49 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default how to get autogenerate value for the ID Field

Hello To All
i want to know how to set the column of table so that it took autogenerated values by sql 2005. e.g i have three fields in table ID,Name,Password. i want Name and Password provided by user and ID must be autogenerated by SQL in table

Plz Tell

thanks......
__________________
thanks......
 
Old December 5th, 2006, 03:20 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

IDENTITY column... look it up in Books OnLine.

--Jeff Moden
 
Old December 6th, 2006, 08:27 AM
Registered User
 
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to CH011KI
Default

Hi Munish
As Jeff says it is worth checking the Books Online But if you are scripting the Table you should end up with something near to this

BEGIN TRANSACTION
    SET QUOTED_IDENTIFIER ON
    SET ARITHABORT ON
    SET NUMERIC_ROUNDABORT OFF
    SET CONCAT_NULL_YIELDS_NULL ON
    SET ANSI_NULLS ON
    SET ANSI_PADDING ON
    SET ANSI_WARNINGS ON
COMMIT TRANSACTION

BEGIN TRANSACTION
GO
CREATE TABLE dbo.TableNameOfYourChoosing
    (
    /*
    [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),

    --Column [Name], DataType Varchar(50) - specify max length
    [Name] varchar(50) NULL,
    --Password of DataType Varchar(50) again you can specify lengh by replaing a value where the 50 appears. Note: A Max of 255
    [Password] varchar(50) NULL
    ) ON [PRIMARY]
GO
COMMIT



Hope this Helps

Thanks
Chris

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

Hi Chris,
your code absolutelu works fine and applied it. Thanks a Lot!



thanks......
 
Old December 6th, 2006, 11:21 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Yep... And if you are using a GUI, use Scope_Identity() to find the ID of a row you just inserted if you need to.

--Jeff Moden
 
Old December 8th, 2006, 03:14 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello
as helped to me to get autogenerate numbers works for me i get another way to do this in sql server object browser-right click on any table and click modify-it will open table columns on right side.
now click on column name to whom you want to apply autogenerate number, when you click the properties window will be displayed below-it have one property called "IsIdentity" click on it and select "Yes" the field have now auto generate number. you can also select initial value as well as seed.it is again very simple if u have already created table and want to apply identity column to field, no need to perform alter command.


thanks......





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ID in hyperlink field pagey ASP.NET 2.0 Basics 4 October 7th, 2007 01:12 PM
'this.ID = id;' in class construction holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 6th, 2006 10:58 AM
How to retrieve ID field of newly-created record Steve777 Classic ASP Professional 3 October 2nd, 2006 11:27 AM
Autogenerate Formview templates wirerider ASP.NET 2.0 Basics 3 August 1st, 2006 06:55 PM
why not index.asp?id=1 can be www.myweb.com/?id=1 BurhanKhan Classic ASP Professional 11 September 6th, 2004 02:06 PM





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