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,863 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

910 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

23 Experts available now in Live!

Get 1:1 Help Now