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 2000 Forums
 SQL Server Development (2000)
 Problem with an auto-incrementer

Author  Topic 

murrayb3024
Yak Posting Veteran

79 Posts

Posted - 2005-04-15 : 16:06:52
I have a query built that will insert over 1000 records into a table, but my problem I just found out is that the table has a key field that is an auto-incrementer, so I can't insert a null. How can I run my query and fill in that field?

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-04-15 : 16:09:54
simply do not insert anything into that column ... skip it.

- Jeff
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-04-15 : 16:10:40
If it is an identity column, then you don't pass anything to it; you let SQL Server handle it. So let's you say you have this table:

CREATE TABLE Table1 (Table1_ID int IDENTITY(1, 1) NOT NULL, SomeColumn varchar(20))

So the first column is auto incrementing, the second is varchar(20). Then to SQL Server handle the auto-incrementing, we do this:

INSERT INTO Table1(SomeColumn) VALUES('Tara')

Now let's look at it:
SELECT * FROM Table1

You should see 1, Tara

If, however, you need to explicitly add your own values, then you use the SET IDENITY_INSERT option, like this:

SET IDENTITY_INSERT Table1 ON

INSERT INTO Table1(Table1_ID, SomeColumn) VALUES(2, 'Mike')

SET IDENTITY_INSERT Table1 OFF

Clean up:
DROP TABLE Table1

Tara
Go to Top of Page

murrayb3024
Yak Posting Veteran

79 Posts

Posted - 2005-04-15 : 16:20:51
I am idiot. I thought there was something funky with it not filling in for me. I was using a copy of the original table and the auto-incrementing was shut off. Sorry for wasting your time. Thank you for reading my post and replying.
Go to Top of Page
   

- Advertisement -