October 27, 2008 at 9:58 am
I have two databases running on the same SQL server. In each database is an identical table with different contents. I want to copy certain rows from one database and insert the rows into the other database. I'm using the query:
insert into vqsmtest.dbo.ithelp_user select * from vqsmtest2.dbo.ithelp_user where comments like 'Oakmead%'
When I run the query I get the following error:
An explicit value for the identity column in table 'vqsmtest.dbo.ithelp_user' can only be specified when a column list is used and IDENTITY_INSERT is ON.
What does this mean? There is a primary key column in each table that must have a unique entry. How can I get the data copied?
October 27, 2008 at 10:15 am
you have to use a list of columns that does not include the primary key, not SELECT *, because the PK already exists:
insert into vqsmtest.dbo.ithelp_user (COL1,COL2,COL3...)
select COL1,COL2,COL3... from vqsmtest2.dbo.ithelp_user
where comments like 'Oakmead%'
Lowell
October 27, 2008 at 10:22 am
Or use SET IDENTITY_INSERT ON
In either case, you can't use select *, you need to specify columns at least in the INSERT part of the statement.
November 5, 2008 at 8:56 am
Agree, use IDENTITY_INSERT ON, and don't for get to issue the OFF command.
And for the select, if possible, use a where command that ensures that the id's being inserted do not exist, as follows:
set identity_insert target_table ON
insert into target_table (myid, mycolumn)
select myid, mycolumn from sourcedatabase..source_table
where myid not in ( select myid from targetdatabase..target_table)
set identity_insert target_table OFF
The more you are prepared, the less you need it.
November 5, 2008 at 9:38 pm
And pray that the IDENTITY column isn't a PK...;)
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply