I have to create a stored procedure to create an invoice table entry. The procedure should allow the user to insert a record by providing a new invoice number x and customer id y. The date should be the current system date and the subtotal, tax and total fields should be 0.
I have written this code for this question:
SET SERVEROUTPUT ON
CREATE OR REPLACE PROCEDURE InoviceEntry_step1(X in number, Y in number)
as
begin
insert into INVOICE(inv_number, cus_code, inv_date,
inv_subtotal, inv_tax, inv_total)
values (X, Y, sysdate, 0, 0, 0);
end;
/
CALL InoviceEntry_step1 (1009,11111);
The call raises the error:
Error report - SQL Error: ORA-02291: integrity constraint (USER.SYS_C00109369) violated - parent key not found ORA-06512: at "USER.INOVICEENTRY_STEP1", line 6 02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found" *Cause: A foreign key value has no matching primary key value. *Action: Delete the foreign key or add a matching primary key.
What am I doing wrong and could there be a better way to do this possibly?
Maybe, you are faced with uncontrolled input situation. Your program need to control them by itself. There are 2 way for handling error.
First, you can handle the error by Exceptions.
Second, you can handle the error by yourself. Example, I check "cus_code" in customer table before insert into invoice.
However, Don't forgot to "commit" the transactions.