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
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 inserting a autonumber column

Author  Topic 

rob41
Yak Posting Veteran

67 Posts

Posted - 2009-12-10 : 17:51:03
I was wondering is it possible in SQL Server 2005 to insert a autonumber field similar to what's done in access?

INSERT INTO [robert_thurs_12102009]
[supplier_id], [supplier_name]
FROM
[robert_thurs_12102009]
VALUES
(supplier_seq.NEXTVAL, 'K Foods');

am I headed down the right path?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-10 : 18:00:31
That's not how it works in SQL Server. Here's an example:

CREATE TABLE t1 (c1 int IDENTITY(1, 1), c2 varchar(5))

INSERT INTO t1 (c2) VALUES ('Test')

SELECT * FROM t1

DROP TABLE t1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

behrman
Yak Posting Veteran

76 Posts

Posted - 2009-12-25 : 21:45:24
alter table add id int identity (1,1) not null

create table a
(
id int identity (1,1) not null ,
...
)

It can take any exact numeric - decimal, bigint, int, smallint, ...

Remember also to put a unique index on the column as it is not guaranteed to be unique.

RAQ Report: Web-based Excel-like Java reporting tool
Go to Top of Page
   

- Advertisement -