database - MYSQL Insert Data into table using Foreign Keys from different tables -
i have temporary table "teststepdump" created "load data local infile". temporary table contains data of different tables connected foreign keys in table testresult:
- teststepdump (dummyno, stationno, name, result)
- duttest (id, dutid, processtime)
- dut (id, dummynr)
- station (id, stationno)
- teststepname(id, name)
- teststepresult(duttestid,teststepid,result)
i´m trying way using stored procedure, error 1452:
sql fehler (1452): cannot add or update child row: foreign key constraint fails (
database
.teststepresult
, constraintteststepresult_ibfk_1
foreign key (duttestid
) referencesduttest
(id
) on delete cascade on update cascade)
begin declare dt_id int; select teststepresult.duttestid dt_id teststepresult inner join duttest on teststepresult.duttestid = duttest.id inner join dut on duttest.dutid = dut.id inner join station on duttest.stationid = station.nummer inner join teststepdump on teststepresult.id = teststepdump.id dut.dummynr = teststepdump.dummyno , station.nummer = teststepdump.stationno; insert teststepresult (duttestid, teststepid, result) select dt_id, teststepname.id, teststepdump.result teststepdump inner join teststepname on teststepdump.name = teststepname.name teststepname.name = teststepdump.name; terminate teststepdump; end
for single insert stored procedure working me, want avoid "for" loop;:
begin declare t_id int; declare tn_id int; select teststepname.id tn_id teststepname teststepname.name = name; select duttest.id t_id duttest inner join dut on duttest.dutid = dut.id inner join station on duttest.stationid = station.id dut.dummynr = dummyno , station.nummer = stationno; if not t_id null , not tn_id null insert teststepresult (duttestid, teststepid, result) values (t_id, tn_id, result); end if; end
i solved problem !
begin insert teststepresult (duttestid, stepname, result) select duttest.dutid, teststepdump.name, teststepdump.result duttest inner join dut on duttest.dutid = dut.id inner join station on duttest.stationid = station.id inner join teststepdump on dut.dummynr = teststepdump.dummyno dut.dummynr = teststepdump.dummyno , station.nummer = teststepdump.stationno; #truncate teststepdump; end
Comments
Post a Comment