Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT statement conflicted with COLUMN FOREIGN KEY constraint

Status
Not open for further replies.

largegrape

Programmer
May 6, 2004
9
0
0
US
I'm trying to update data from Database1 to Database2. Both databases have the same schema. Table1.column3_foreign_key has a foreign key constraint Table2.column1_primary_key (lookup table). Table2.column1 is a primary key with identity. The problem is that when I try to do something like this:

insert into Database2.dbo.Table1
(
column2
column3_foreign_key
)
select
column2
column3_foreign_key
from Database1.dbo.Table1

Database2.dbo.Table2.column1_primary_key
has different values than
Database1.dbo.Table2.column1_primary_key

How do I insert the data with out constraint error so I can
do an update to correct the links in Database2 aftwards?

Thanks,
LG

 
Remove the constraint.
Correct the data.
Add the contraint.

Better to add the data correctly instead.

Something like
insert into Database2.dbo.Table1
(
column2
column3_foreign_key
)
select
column2 ,
d2t2.pk
from Database1.dbo.Table1 d1t1
join Database1.dbo.Table2 d1t2
on d1t2.PK = dt1.column3_foreign_key
join Database2.dbo.Table2 d2t2
on d2t2.desc = d1t2.desc


======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
first:
ALTER TABLE TABLE1 NOCHECK CONSTRAINT CONSTRAINT_NAME
and then:
ALTER TABLE TABLE1 CHECK CONSTRAINT CONSTRAINT_NAME

Everything but Nothing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top