Solved

Can not insert into table with Identity insert .

Posted on 2006-06-29
7
1,782 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Viewers will learn how the fundamental information of how to create a table.
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.

747 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

9 Experts available now in Live!

Get 1:1 Help Now