Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Add ID field to a table

Author  Topic 

shifis
Posting Yak Master

157 Posts

Posted - 2006-03-09 : 18:58:29
I need to know how to add an ID column, well actually a colum that start on 1 and continue counting something like a transaccion_number.

Is there a special field that I can uses to do that?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-03-09 : 19:03:53
Yes. You can use the IDENTITY option on a column.

Tara Kizer
aka tduggan
Go to Top of Page

activecrypt
Posting Yak Master

165 Posts

Posted - 2006-03-09 : 21:43:37
Hi,
refer identity and alter table statements in bol :

E.g.:
USE AdventureWorks
IF OBJECT_ID ('dbo.new_employees', 'U') IS NOT NULL
DROP TABLE new_employees
GO
CREATE TABLE new_employees
(
id_num int IDENTITY(1,1),
fname varchar (20),
minit char(1),
lname varchar(30)
)

INSERT new_employees
(fname, minit, lname)
VALUES
('Karin', 'F', 'Josephs')

INSERT new_employees
(fname, minit, lname)
VALUES
('Pirkko', 'O', 'Koskitalo')

HTH
Regards

Andy Davis
Sql Shield Team
--------------------------------------------
SQL Server Encryption Software
http://www.sql-shield.com
Go to Top of Page

a_r_satish
Yak Posting Veteran

84 Posts

Posted - 2006-03-13 : 06:22:32
Use identity(seed,increment). In Seed specify the starting value and in the increment use the incremental value.

Regards,
satish.r
"Known is a drop, Unknown is an Ocean"
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-03-13 : 07:39:15
alter table mytable add seq int identity(1,1) not null

Note that this will add the identity column and populate it which can take a long time for a big table - during this time the table will be locked.
You will also need to add a unique index to the column to ensure uniqueness.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -