Solved

Can not insert into table with Identity insert .

Posted on 2006-06-29
7
1,787 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 143

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

830 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