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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert not allowed due to a relationship

Status
Not open for further replies.

easycode

Programmer
Jan 28, 2005
195
US
this is gonna be hard to explain, but here it is.
i have this to 2 tables linked by custid (customer-id)
HistDet Customer
-custid -custid
-product -name
-subject -address

i need to transfer information from another database (it is just an upgrade bewtween versions).

Then Database1 have Histdet and Customer (New version)
and Database2 have Histdet and Customer (Old version)

i need to transfer tables from Database2 to Database1

i copied all tables from database2 as link tables into database1.

then in database1 i have:
-Histdet (from database1)
-Customer (from database1)
-Histdet1 (from database2)
-Customer1 (from database2)

i am running this instruction

insert into Customer select * from customer1

Then i am getting the next error message:
"You cannot add or change a record because a related record is required in table 'HistDet'."
and the error # is : -2147467259

this means that a record in customer1 from database2 is not found in histdet from database1

I need to know how can i control this to copy only the records from customer1 that match relatiionship in customer and histdet

I hope you got it, if more information is needed please let me know. Thanks for your help
 
You will need to check that the ID exist in Customer1. Perhaps something like (rough suggestion - needs work):
INSERT INTO Customer ( [Field1], [Field2] )
SELECT Customer1.[Field1], Customer1.[Field2]
FROM Customer1 INNER JOIN HistDet ON Customer1.HistDetID = HistDet.ID;
But please check - the above does not come with any guarantees!
 
As Remou said or,

INSERT INTO Customer SELECT * FROM Customer1 WHERE pkID IN (SELECT fkID FROM HistDet)

Something like this...


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top