tags:

views:

60

answers:

2

I am using following query to bulk insert from one table to another.

INSERT INTO billitems SELECT * FROM billitems_old;

I want that if insert fails on any row, it must skip that row and proceed. Is there anything that I can include in the above query to skip errors.

+2  A: 
insert ignore into billitems select * from billitems_old;

reference: insert

shylent
not working. still showing errors and terminating.
RPK
what kind of errors? can't you be be more specific? (perhaps update your question with this info)
shylent
Foreign key error that it was originally showing. It is stopping on errors, when I want it to skip it.
RPK
shylent
Here is the error: "Cannot add or update a child row: a foreign key constraint fails (`stockist`.`billitems`, CONSTRAINT `FK_billitems_1` FOREIGN KEY (`BillNo`, `BillDate`) REFERENCES `billdetails` (`BillNo`, `BillDate`) ON DELETE CASCADE ON UPDATE CASCADE)"
RPK
Well, as far as I know, this can be circumvented by setting `foreign_key_checks` to 0 before the insert. Re-enable it afterwards.
shylent
A: 

From the online documentation:

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead.

So try:

INSERT IGNORE INTO billitems SELECT * FROM billitems_old
Andomar