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,909 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 69

Accepted Solution

by:
Scott Pletcher 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

832 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