Solved

An explicit value for the identity column in table 'dbTest.dbo.table' can only be specified when a column list is used and IDENTITY_INSERT is ON.

Posted on 2013-01-17
7
1,819 Views
Last Modified: 2013-01-17
I have an insert from one table to another and I keep getting this error.  The destination table has a autoincrementing column and I need it to continue that count.  PLease help.
0
Comment
Question by:rxresults
7 Comments
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 250 total points
ID: 38788139
What's the insert statement / table definition look like ?

Sounds like you're trying to set the value of the identity column yourself ?

If you're wanting the identity column to do it's thing and just auto increment, don't specify that column in the insert statement.
0
 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 125 total points
ID: 38788182
ONLY if you DONT need to keep the IDENTITY sequence and MUST insert specific values you can run a SET like below otherwise code your INSERT statement WITHOUT the identity column in the list:

SET IDENTITY_INSERT Table1 ON

against that table and tur it OFF after the insert. More details at:

http://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity
0
 

Author Comment

by:rxresults
ID: 38788837
Here is what I am using and I still get the error: The primary key (which is also the autoincrementing column) is missing from this query


set identity_Insert 'dbTest.dbo.table'  on
go
insert into  'dbTest.dbo.table'
SELECT
       [ValidRx] as [ValidRx]
      ,[ImportOrgId] as [ImportOrgId]
      ,[CustomerId] as [CustomerId]
      ,[AccountId] as [AccountId]
      ,[GroupId] as [GroupId]
      ,[UtilizationDate] as [UtilizationDate]
      ,[ProcessDate] as [ProcessDate]
   FROM 'dbTest.dbo.table'
  go
 
  set identity_Insert 'dbTest.dbo.table'  off
go
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 69

Accepted Solution

by:
ScottPletcher earned 125 total points
ID: 38788901
Re-read the error message carefully:

"when a column list is used and ..."


set identity_Insert dbTest.dbo.table  on
go
insert into  dbTest.dbo.table (
   [ValidRx], [ImportOrgId], [CustomerId], ...  --<<-- as msg states, you must list columns
)
SELECT
    ...
0
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 250 total points
ID: 38788909
Try specifying the column names in your insert rather than allowing them to default ?

insert into table (col1, col2, col3) select (cola, colb, colc)

Does that make any difference ?
0
 

Author Comment

by:rxresults
ID: 38788968
I tried it and now the message has changed slightly:

Explicit value must be specified for identity column in table 'dbo.table'  either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38789146
Don't turn IDENTITY_INSERT ON when you're trying to have the identity do it's job automatically.
0

Featured Post

What Security Threats Are You Missing?

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.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

760 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

16 Experts available now in Live!

Get 1:1 Help Now