Solved

Can not insert into table with Identity insert .

Posted on 2006-06-29
7
1,788 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

752 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