?
Solved

Can not insert into table with Identity insert .

Posted on 2006-06-29
7
Medium Priority
?
1,792 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 43

Assisted Solution

by:TimCottee
TimCottee earned 400 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1600 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

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 143

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

718 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