Solved

Can not insert into table with Identity insert .

Posted on 2006-06-29
7
1,783 Views
Last Modified: 2008-01-09
Hi, I have a code to insert and some how I could not insert properly.  I found out that the table I use has a column that is defined as Identity increament. Currently my table has only 3 roles so the column with identity has entry 1, 2, 3.
So to test this I went to the query analyzer and type the following SQL statement:

SET IDENTITY_INSERT fintracreport OFF
insert into fintracreport values(5,'1',1,'1',1)

Yet I still get the following error:

Server: Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'fintracreport' can only be specified when a column list is used and IDENTITY_INSERT is ON.

Any ideas about this? any helps are appriciated.
0
Comment
Question by:fylix0000
  • 3
  • 3
7 Comments
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 100 total points
ID: 17009682
Hi fylix0000,

That is because you need to set it on not off:

SET IDENTITY_INSERT fintracreport ON
insert into fintracreport values(5,'1',1,'1',1)
SET IDENTITY_INSERT fintracreport OFF

Tim Cottee
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 17009693
this should work better:
insert into fintracreport (col1, col2, col3, col4) values(5,'1',1,'1',1)

you have to list the columns to which you want to put the values to, ie all columns except the one that is identity
0
 

Author Comment

by:fylix0000
ID: 17009698
Hmm,

I pasted your code and run all at the same time and yet still get same error.

SET IDENTITY_INSERT fintracreport ON
insert into fintracreport values(5,'1',1,'1',1)
SET IDENTITY_INSERT fintracreport OFF


Server: Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'fintracreport' can only be specified when a column list is used and IDENTITY_INSERT is ON.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17009718
in both cases you have to specify the column list.
0
 

Author Comment

by:fylix0000
ID: 17009735
I see.

So I followed Angel and did this and seems to work.:

SET IDENTITY_INSERT fintracreport ON
insert into fintracreport ("id", sequence_id, type, report_file,"time") values(5,'1',1,'1',1)



One more small question, now that i have 1,2,3,5 in my identity, if I insert another column, would it start with 6 or 4?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17009788
>One more small question, now that i have 1,2,3,5 in my identity, if I insert another column, would it start with 6 or 4?

after inserting values manually into a identity field, you need to run the following statement to ensure the identity will continue correct:

DBCC CHECKIDENT (yourtable, RESEED)
0
 

Author Comment

by:fylix0000
ID: 17009804
Great, thank you, I learned something new :)
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now