Link to home
Start Free TrialLog in
Avatar of crazy4s
crazy4s

asked on

Error Execute Package.Procedure in PL/SQL

Hi,
I'm getting tons of errors when doing this execute which call the procedures to insert this new data into the specified tables....
this is linked to the previous post which is the package and package body with the procedure Do1Order have been created....

some of these errors were repeated so i just post once...

the tables:
CREATE TABLE OrderLog (
  Order# INTEGER
    check (Order# > 1000 and Order# < 10000)
    CONSTRAINT Order#_pk_OrderLog PRIMARY KEY,
  OrderDateTime DATE,
  Cust# INTEGER
    check (Cust# > 100 and Cust# < 1000)
    CONSTRAINT Customer_fk_OrderLog
    REFERENCES Customer(CustomerID),
  OrderTotalPrice INTEGER
);

CREATE TABLE ErrorLog (
  Order# INTEGER
    check (Order# > 1000 and Order# < 10000)
    CONSTRAINT Order#_pk PRIMARY KEY,
  OrderDateTime DATE,
  Cust# INTEGER
    check (Cust# > 100 and Cust# < 1000)
    CONSTRAINT Customer_fk_ErrorLog
    REFERENCES Customer(CustomerID),
  Prod# INTEGER
    check (Prod# > 10000 and Prod# < 100000)
    CONSTRAINT Product_fk_ErrorLog
    REFERENCES Product(ProductID),
  Quan INTEGER,
  ErrorMessage VARCHAR2(50)
);

CREATE TABLE CustInvoice (
  Invoice# INTEGER
    CONSTRAINT Invoice_pk PRIMARY KEY,
  Cust# INTEGER
    check (Cust# > 100 and Cust# < 1000)
    REFERENCES Customer(CustomerID),
  CustName VARCHAR2(25),
  Street VARCHAR2(25),
  Zip INTEGER
    check (Zip > 0 and Zip < 100000)
    CONSTRAINT Zip_fk_CustInvoice
    REFERENCES Zipcode(Zip)
);

the 3 triggers:
--start "C:\Users\jessica99327\AppData\Roaming\SQL Developer\StartSequences.sql"

CREATE OR REPLACE TRIGGER CheckQuan
AFTER UPDATE OF QuanInStock
ON Product
FOR EACH ROW
BEGIN
  IF :old.QuanInStock <= :old.ReorderPoint THEN
      INSERT INTO
        ReOrder (Prod#, RequestDateTime, NormalQuan, ReorderQuan)
      VALUES
        (:old.ProductID, SYSDATE, 'T', :old.ReorderQuan);
       
  END IF;
   
END;
/

show errors

CREATE OR REPLACE TRIGGER DoCustInvoice
AFTER INSERT OR UPDATE ON ProductOrder
FOR EACH ROW
DECLARE
  v_action VARCHAR(50);
BEGIN
  IF UPDATING THEN
    v_action := 'A row has been updated in the ProductOrder table';
  END IF;
  IF INSERTING THEN
    v_action := 'A row has been inserted in the ProductOrder table';
  END IF;
INSERT INTO
    CustInvoice (Invoice#, Cust#, CustName, Street, Zip)
    select InvoiceSEQ.nextval, :new.CustomerID, C.FirstName || C.LastName, C.Street, C.Zip from Customer C where C.CustomerID = :new.CustomerID;
END;
/

show errors

CREATE OR REPLACE TRIGGER DoPo
AFTER INSERT OR UPDATE ON ReOrder
FOR EACH ROW
DECLARE
  v_action VARCHAR2(50);
BEGIN
  IF UPDATING THEN
    v_action := 'A row has been updated in the ReOrder table';
  END IF;
  IF INSERTING THEN
    v_action := 'A row has been inserted in the ReOrder table';
  END IF;
  INSERT INTO
    PurchaseOrder (PONo, ProductID, PODate)
    VALUES
    (PONoSEQ.nextval, :new.Prod#, :new.RequestDateTime);  
END;
/

show errors

SQL> execute OrderProcessingDef.Do1Order(101, 10010, 100, 10014, 1,'CC');
BEGIN OrderProcessingDef.Do1Order(101, 10010, 100, 10014, 1,'CC'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.INVOICE_PK) violated
ORA-06512: at "JESSICAFOO9.DOCUSTINVOICE", line 10
ORA-04088: error during execution of trigger 'JESSICAFOO9.DOCUSTINVOICE'
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 11
ORA-06512: at line


SQL> execute OrderProcessingDef.Do1Order(102, 99999, 1, 10014, 1,'FOOD');
BEGIN OrderProcessingDef.Do1Order(102, 99999, 1, 10014, 1,'FOOD'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated
- parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 13
ORA-06512: at line 1
ASKER CERTIFIED SOLUTION
Avatar of OP_Zaharin
OP_Zaharin
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crazy4s
crazy4s

ASKER

yes i realized that as the productID wasn't in the product table... and so I should define this as an error msg in my procedure in the package saying that it was a bad product number right?
OP is right, but the parent fails because the trigger fails, check the query in the trigger for correct return values.
Avatar of crazy4s

ASKER

and what is the other error means:
SQL> execute OrderProcessingDef.Do1Order(103, 10010, 1, 10014, 900, 'Cash');
BEGIN OrderProcessingDef.Do1Order(103, 10010, 1, 10014, 900, 'Cash'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.INVOICE_PK) violated
ORA-06512: at "JESSICAFOO9.DOCUSTINVOICE", line 10
ORA-04088: error during execution of trigger 'JESSICAFOO9.DOCUSTINVOICE'
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 11
ORA-06512: at line 1
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure wether you should error out, i'd insert 0 for those values so that the order is entered anyhow with an invalid product.
Avatar of crazy4s

ASKER

this is the trigger but it runs fine....

CREATE OR REPLACE TRIGGER DoCustInvoice
AFTER INSERT OR UPDATE ON ProductOrder
FOR EACH ROW
DECLARE
  v_action VARCHAR(50);
BEGIN
  IF UPDATING THEN
    v_action := 'A row has been updated in the ProductOrder table';
  END IF;
  IF INSERTING THEN
    v_action := 'A row has been inserted in the ProductOrder table';
  END IF;
INSERT INTO
    CustInvoice (Invoice#, Cust#, CustName, Street, Zip)
    select InvoiceSEQ.nextval, :new.CustomerID, C.FirstName || C.LastName, C.Street, C.Zip from Customer C where C.CustomerID = :new.CustomerID;
END;
/
- you should separate this question as its 2 different errors altogether. it will be difficult for the experts to answer 2 different question in one posting. could you kindly choose which error do you want to solve in this posting?
Avatar of crazy4s

ASKER

yes because i created a table for the ErrorLog whenever there's any bad cust# or prod# this procedures will have to insert a row into the ErrorLog table.... okay then i'll continue ask the error msg in another post....
for now...i think the integrity constraint error most likely is related to the bad prod# but for the 2nd error...i can't really figure out...and is stated there related to the trigger...
Avatar of crazy4s

ASKER

sorry i'll choose this
SQL> execute OrderProcessingDef.Do1Order(103, 10010, 1, 10014, 900, 'Cash');
BEGIN OrderProcessingDef.Do1Order(103, 10010, 1, 10014, 900, 'Cash'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.INVOICE_PK) violated
ORA-06512: at "JESSICAFOO9.DOCUSTINVOICE", line 10
ORA-04088: error during execution of trigger 'JESSICAFOO9.DOCUSTINVOICE'
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 11
ORA-06512: at line 1
- ok great :)
- as i've pointed earlier, the value you are inserting, is into a primary key column and the value is already exist in that table.lets check for the value that you are inserting, does it exists in the table? you should give a different value.
- which table and column does this primary key INVOICE_PK belong to? you should look into that.
- furthermore i notice in all your insert inside the package is inserting OrderSEQ.nextval. they MIGHT using the same OrderSEQ value. you should put commit in-between the insert line as i pointed much earlier.
Avatar of crazy4s

ASKER

the related table

CREATE TABLE CustInvoice (
  Invoice# INTEGER
    CONSTRAINT Invoice_pk PRIMARY KEY,
  Cust# INTEGER
    check (Cust# > 100 and Cust# < 1000)
    REFERENCES Customer(CustomerID),
  CustName VARCHAR2(25),
  Street VARCHAR2(25),
  Zip INTEGER
    check (Zip > 0 and Zip < 100000)
    CONSTRAINT Zip_fk_CustInvoice
    REFERENCES Zipcode(Zip)
);

the related trigger:
CREATE OR REPLACE TRIGGER DoCustInvoice
AFTER INSERT OR UPDATE ON ProductOrder
FOR EACH ROW
DECLARE
  v_action VARCHAR(50);
BEGIN
  IF UPDATING THEN
    v_action := 'A row has been updated in the ProductOrder table';
  END IF;
  IF INSERTING THEN
    v_action := 'A row has been inserted in the ProductOrder table';
  END IF;
INSERT INTO
    CustInvoice (Invoice#, Cust#, CustName, Street, Zip)
    select InvoiceSEQ.nextval, :new.CustomerID, C.FirstName || C.LastName, C.Street, C.Zip from Customer C where C.CustomerID = :new.CustomerID;
END;
/

and so i just need to add in commit after every insert...
Avatar of crazy4s

ASKER

>>as i've pointed earlier, the value you are inserting, is into a primary key column and the value is already exist in that table.lets check for the value that you are inserting, does it exists in the table? you should give a different value.

execute OrderProcessingDef.Do1Order(103, 10010, 1, 10014, 900, 'Cash');
THE 103 is the cust#  and this customerID is already exists in the Customer table(which has customerID col - primary key) is it because of this reason that causes the error?
- i just realize this while trying to understanding your trigger and package. the error is in the trigger while inserting into ProductOrder in the packages. check InvoiceSEQ.nextval.

ORA-06512: at "JESSICAFOO9.DOCUSTINVOICE", line 10
ORA-04088: error during execution of trigger 'JESSICAFOO9.DOCUSTINVOICE'

Avatar of crazy4s

ASKER

the InvoiceSEQ.nextval is automatically generated from 101, it's a sequence... so whenever there's any inserts in ProductOrder it'll automatically create an entry in the CustInvoice table...while this DoNewOrder inserts new data into the ProductTable...
- true. my suggestion for you to check the value current value of the InvoiceSEQ sequence. i suspecting the value is already exist in Invoice# column on CustInvoice table.
Avatar of crazy4s

ASKER

yup before inserting these DoNewOrders data... the CustInvoice table already have the data from the existing data in the ProductOrder table...so the Invoice# have already started before this DoNewOrders inserts... but shouldn't the DoNewOrders insert will continue the next invoice# like let's say the first batch of insert till 106 and so when this DoNewOrders inserts it'll continue at 107 and so on?
- true. however you might have somehow insert the value manually into the table before or the nextvalue is not moving to the next as it should be. look for how to change the value of the sequence to the next value without using the trigger, then try again.
Avatar of crazy4s

ASKER

>>change the value of the sequence to the next value without using the trigger, then try again.
hmm how can i change the value of sequence in this case?? i got no idea on this....
or can i create a same sequence (well i think it won't work hmmm???)
- as this is an assignments, i can't really give you the syntax. but you can look for alter sequence for setting the new sequence value.
Avatar of crazy4s

ASKER

hmm i tried to add in ALTER SEQUENCE InvoiceSEQ increment by 1;
before the DoNewOrders INSERTS to make sure the Invoice SEQ is incremented but i still get the same error??
- increase the number until the latest sequence number does not match with any of the invoice#.
- find the syntax to query the current value of the InvoiceSEQ so that you know how much to increase.
Avatar of crazy4s

ASKER

okay after double check...
after doing the execute for DoNewOrders... the Invoice# continue from the previous (before the DoNewOrder is until 106 and after the DoNewOrders it started from 107 till 111 but actual valid data should be until 114 (which have 8 valid data)....
- so does it work for you now? i can't quite catch "but actual valid data should be until 114 (which have 8 valid data).."
Avatar of crazy4s

ASKER

well i still get the same errors but when i do select * from CustInvoice order by Invoice#;
i saw some new entries in the CustInvoice table so i'm expecting it's inserting the some of the new data from the DoNewOrders but not all...
- lets settle this issue first. can you paste the error message?
Avatar of crazy4s

ASKER

SQL> execute OrderProcessingDef.Do1Order(101, 10010, 3, 10011, 5, 'CC');
BEGIN OrderProcessingDef.Do1Order(101, 10010, 3, 10011, 5, 'CC'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 46
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(102, 10011, 12, 10010, 1,'Check');
BEGIN OrderProcessingDef.Do1Order(102, 10011, 12, 10010, 1,'Check'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 46
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(101, 10010, 1, 10012, 15, '');
BEGIN OrderProcessingDef.Do1Order(101, 10010, 1, 10012, 15, ''); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 46
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(103, 10015, 17, 10021, 19,'CASH');
BEGIN OrderProcessingDef.Do1Order(103, 10015, 17, 10021, 19,'CASH'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 46
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(107, 10010, 1, 10013,1,'CASH');
BEGIN OrderProcessingDef.Do1Order(107, 10010, 1, 10013,1,'CASH'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 17
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_PRODUCTORDER) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(101, 10010, 100, 10014, 1,'CC');
BEGIN OrderProcessingDef.Do1Order(101, 10010, 100, 10014, 1,'CC'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 46
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(103, 10010, 1, 10014, 900, 'Cash');
BEGIN OrderProcessingDef.Do1Order(103, 10010, 1, 10014, 900, 'Cash'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 46
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(102, 99999, 1, 10014, 1,'FOOD');
BEGIN OrderProcessingDef.Do1Order(102, 99999, 1, 10014, 1,'FOOD'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 28
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(105, 10014, 1, 99999, 1, 'CC');
BEGIN OrderProcessingDef.Do1Order(105, 10014, 1, 99999, 1, 'CC'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 38
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(103, 99999, 1, 10014, 100,'Check');
BEGIN OrderProcessingDef.Do1Order(103, 99999, 1, 10014, 100,'Check'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 28
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(104, 10012, 1000, 99343, 100,'CHECK');
BEGIN OrderProcessingDef.Do1Order(104, 10012, 1000, 99343, 100,'CHECK'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 38
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(443, 10012, 1, 42342, 5, 'CC');
BEGIN OrderProcessingDef.Do1Order(443, 10012, 1, 42342, 5, 'CC'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 17
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_PRODUCTORDER) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(999, 32422, 4, 10015, 1, 'CC');
BEGIN OrderProcessingDef.Do1Order(999, 32422, 4, 10015, 1, 'CC'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 17
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_PRODUCTORDER) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(322, 10012, 1000, 10015, 2,'Check');
BEGIN OrderProcessingDef.Do1Order(322, 10012, 1000, 10015, 2,'Check'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 17
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_PRODUCTORDER) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(422, 10012, 1, 10015, 400, 'Cash');
BEGIN OrderProcessingDef.Do1Order(422, 10012, 1, 10015, 400, 'Cash'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 17
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_PRODUCTORDER) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(110, 99999, 1, 10000, 1,'SFS');
BEGIN OrderProcessingDef.Do1Order(110, 99999, 1, 10000, 1,'SFS'); END;

*
ERROR at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ERRORLOG) violated -
parent key not found
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 17
ORA-02291: integrity constraint (JESSICAFOO9.CUSTOMER_FK_PRODUCTORDER) violated
- parent key not found
ORA-06512: at line 1


SQL> execute OrderProcessingDef.Do1Order(103, 10015, 5, 10011, 1,'CC');
BEGIN OrderProcessingDef.Do1Order(103, 10015, 5, 10011, 1,'CC'); END;

*
ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 46
ORA-06512: at line 1
Avatar of crazy4s

ASKER

i'm not sure with the unique constraint error but those integrity constraint error are those with bad cust# and prod#...
- now in your original package without the exception. can you compile? if success, execute only 1 transaction and paste the error.

CREATE OR REPLACE PACKAGE BODY OrderProcessingDef AS
   PROCEDURE Do1Order
    (Cust# IN INTEGER,
     Prod1# IN INTEGER,
     Quan1 IN INTEGER,
     Prod2# IN INTEGER,
     Quan2 IN INTEGER,
     PayType IN VARCHAR2)
  IS
  BEGIN
    INSERT INTO ProductOrder (OrderID, OrderDate, ShipDate, PaymentType,CustomerID)      
      values (OrderSEQ.nextval, SYSDATE, NULL, 'CC', Cust#);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)      
      values (OrderSEQ.nextval, Prod1#, Quan1);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)    
      values (OrderSEQ.nextval, Prod2#, Quan2);
    COMMIT;
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select OrderSEQ.nextval, SYSDATE, Cust#, P.Price * Quan1 from Product P where P.ProductID = Prod1#;
    COMMIT;
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select OrderSEQ.nextval, SYSDATE, Cust#, P.Price * Quan2 from Product P where P.ProductID = Prod2#;
    COMMIT;
  END;  
END;
/
- tell me from you list of insert in the package, which table is inserted successfully and which is not.
Avatar of crazy4s

ASKER

do you meant the execute OrderProcessingDef.Do1Order(101, 10010, 3, 10011, 5, 'CC');
well the package body created but after those execute none of them is successful....
Avatar of crazy4s

ASKER

the above is without the exception in the package body which is created successfully.
1-    INSERT INTO ProductOrder (OrderID, OrderDate, ShipDate, PaymentType,CustomerID)      
      values (OrderSEQ.nextval, SYSDATE, NULL, 'CC', Cust#);


2-    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)      
      values (OrderSEQ.nextval, Prod1#, Quan1);

3-    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)    
      values (OrderSEQ.nextval, Prod2#, Quan2);

4-    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select OrderSEQ.nextval, SYSDATE, Cust#, P.Price * Quan1 from Product P where P.ProductID = Prod1#;

5-    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select OrderSEQ.nextval, SYSDATE, Cust#, P.Price * Quan2 from Product P where P.ProductID = Prod2#;


- yes please tell me from the above insert, which one is fail? and which one got data in it?
Avatar of crazy4s

ASKER

ProductOrder have new entries...but OrderDetails and OrderLog have no rows selected.
- ok lets go on OrderDetails error. can you paste error related to OrderDetails?

CREATE OR REPLACE PACKAGE BODY OrderProcessingDef AS
   PROCEDURE Do1Order
    (Cust# IN INTEGER,
     Prod1# IN INTEGER,
     Quan1 IN INTEGER,
     Prod2# IN INTEGER,
     Quan2 IN INTEGER,
     PayType IN VARCHAR2)
  IS
  BEGIN
    INSERT INTO ProductOrder (OrderID, OrderDate, ShipDate, PaymentType,CustomerID)      
      values (OrderSEQ.nextval, SYSDATE, NULL, 'CC', Cust#);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)      
      values (OrderSEQ.nextval, Prod1#, Quan1);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)    
      values (OrderSEQ.nextval, Prod2#, Quan2);
    COMMIT;

/*
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select OrderSEQ.nextval, SYSDATE, Cust#, P.Price * Quan1 from Product P where P.ProductID = Prod1#;
    COMMIT;
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select OrderSEQ.nextval, SYSDATE, Cust#, P.Price * Quan2 from Product P where P.ProductID = Prod2#;
    COMMIT;
*/

  END;  
END;
/

- put a remarks as i do above on both OrderLog then compile. so that we can concentrate on OrderDetail issue first.
Avatar of crazy4s

ASKER

how do i show the OrderDetails error as i created the package body successfully?
"how do i show the OrderDetails error as i created the package body successfully?"

- you can also manually do the insert in sqlplus or any sql tools you have and see what error it prompt:
INSERT INTO OrderDetails (OrderID, ProductID, Quantity)  values (OrderSEQ.nextval, X, X);
Avatar of crazy4s

ASKER

while i tried to do insert into Orderetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 99999, 10);

Error at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated -\
parent key not found
Avatar of crazy4s

ASKER

while i tried with a valid productID
insert into Orderetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 10011, 10);

Error at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.ORDER_FK_ORDERDETAILS) violated -\
parent key not found

my OrderDetails table:
CREATE TABLE OrderDetails (
  OrderID INTEGER
    check (OrderID > 1000 and OrderID < 10000)
    CONSTRAINT Order_fk_OrderDetails
    REFERENCES ProductOrder(OrderID),
  ProductID INTEGER
    check (ProductID > 10000 and ProductID < 100000)
    CONSTRAINT Product_fk_OrderDetails
    REFERENCES Product(ProductID),
  Quantity INTEGER NOT NULL,
  CONSTRAINT OrderDetails_pk PRIMARY KEY (OrderID, ProductID)
);
Error at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.ORDER_FK_ORDERDETAILS) violated -\
parent key not found

- so its the same error that you got when executing the package with data right?
> now i saw that the problem is you are using OrderSEQ.nextval in all your insert in the packages. so it will create new number for each insert whereelse the number does not exist in the parent ProductOrder(OrderID). if you want to carry the same OrderSEQ.nextval for all the insert, you need to put the OrderSEQ.nextval value into a variable then replace the OrderSEQ.nextval in the insert statement with that variable.
Avatar of crazy4s

ASKER

hmm so do you meant in the package body i should have something like
declare
   v_OrderSEQ = OrderSEQ.nextval
and in all the insert statements replace this OrderSEQ.nextval with v_OrderSEQ???
- great! you got the idea.
- except that you need to select the nextval as a normal query. then assign the result value to the variable.
- use the same query on how you get the latest sequence number.
Avatar of crazy4s

ASKER

>>select the nextval as a normal query. then assign the result value to the variable.
hmm i don't really get here...can you provide some example?
- can you paste how you do query to get the latest sequence value? you can google on this. then you will get the idea of the below syntax.

>>select the nextval as a normal query. then assign the result value to the variable.
SELECT seq INTO variable FROM ...
Avatar of crazy4s

ASKER

while i have these
CREATE SEQUENCE OrderSEQ start with 2001 increment by 1;
CREATE SEQUENCE InvoiceSEQ start with 101 increment by 1;
CREATE SEQUENCE PONoSEQ start with 21 increment by 1;

if i want to get the nextval can i use alter sequence?
ALTER SEQUENCE OrderSEQ increment by 1; ???

SELECT OrderSEQ.nextval into v_OrderSEQ FROM dual??
- yes, the last one is the one that you need. you will also need to declare the variable in the package.
Avatar of crazy4s

ASKER

so where do i supposed to put this select statement?? is it under the INSERT statement??
- you should put it only once, before the first insert. so that the other insert will use the same value.
Avatar of crazy4s

ASKER

SQL> CREATE OR REPLACE PACKAGE BODY OrderProcessingDef AS
  2        PROCEDURE Do1Order
  3         (Cust# IN INTEGER,
  4          Prod1# IN INTEGER,
  5          Quan1 IN INTEGER,
  6          Prod2# IN INTEGER,
  7          Quan2 IN INTEGER,
  8          PayType IN VARCHAR2)
  9    DECLARE
 10            v_OrderSEQ = OrderSEQ.nextval;
 11    BEGIN
 12    SELECT OrderSEQ.nextval into v_OrderSEQ FROM dual;
 13         INSERT INTO ProductOrder (OrderID, OrderDate, ShipDate, PaymentType,CustomerID)
 14           values (v_OrderSEQ, SYSDATE, NULL, 'CC', Cust#);
 15         COMMIT;
 16         INSERT INTO OrderDetails (OrderID, ProductID, Quantity)
 17           values (v_OrderSEQ, Prod1#, Quan1);
 18         COMMIT;
 19         INSERT INTO OrderDetails (OrderID, ProductID, Quantity)
 20           values (v_OrderSEQ, Prod2#, Quan2);
 21         COMMIT;
 22         INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
 23           select v_OrderSEQ, SYSDATE, Cust#, P.Price * Quan1 from Product P where P.ProductID = Prod1#;
 24         COMMIT;
 25         INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
 26           select v_OrderSEQ, SYSDATE, Cust#, P.Price * Quan2 from Product P where P.ProductID = Prod2#;
 27         COMMIT;
 28    END;
 29  END;
 30  /

Warning: Package Body created with compilation errors.

SQL> show errors
Errors for PACKAGE BODY ORDERPROCESSINGDEF:

LINE/COL ERROR
-------- -----------------------------------------------------------------
9/3      PLS-00103: Encountered the symbol "DECLARE" when expecting one of
         the following:
         ; is with authid as cluster order using external
         deterministic parallel_enable pipelined
- declare is not needed. and v_OrderSEQ = OrderSEQ.nextval is not the way you declare an object. you need something like "Cust# Integer" and put it after the CREATE OR REPLACE PACKAGE BODY before the procedure.
Avatar of crazy4s

ASKER

do you meant like this
CREATE OR REPLACE PACKAGE BODY OrderProcessingDef AS
  (v_OrderSEQ IN INTEGER)
   PROCEDURE Do1Order
    (Cust# IN INTEGER,
     Prod1# IN INTEGER,
     Quan1 IN INTEGER,
     Prod2# IN INTEGER,
     Quan2 IN INTEGER,
     PayType IN VARCHAR2)
  BEGIN
  SELECT OrderSEQ.nextval into v_OrderSEQ FROM dual;
    INSERT INTO ProductOrder (OrderID, OrderDate, ShipDate, PaymentType,CustomerID)      
      values (v_OrderSEQ, SYSDATE, NULL, 'CC', Cust#);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)      
      values (v_OrderSEQ, Prod1#, Quan1);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)    
      values (v_OrderSEQ, Prod2#, Quan2);
    COMMIT;
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select v_OrderSEQ, SYSDATE, Cust#, P.Price * Quan1 from Product P where P.ProductID = Prod1#;
    COMMIT;
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select v_OrderSEQ, SYSDATE, Cust#, P.Price * Quan2 from Product P where P.ProductID = Prod2#;
    COMMIT;
  END;  
END;
/
- the position is correct, you don't need the bracket () and you missing the semicolon at the end.
- i see that you also missing "IS". it should be there, check the original package and place the IS back.
Avatar of crazy4s

ASKER

hmm i still get the same error for the BEGIN
CREATE OR REPLACE PACKAGE BODY OrderProcessingDef AS
  v_OrderSEQ INTEGER;
   PROCEDURE Do1Order
    (Cust# IN INTEGER,
     Prod1# IN INTEGER,
     Quan1 IN INTEGER,
     Prod2# IN INTEGER,
     Quan2 IN INTEGER,
     PayType IN VARCHAR2)
  BEGIN
  SELECT OrderSEQ.nextval into v_OrderSEQ FROM dual;
    INSERT INTO ProductOrder (OrderID, OrderDate, ShipDate, PaymentType,CustomerID)      
      values (v_OrderSEQ, SYSDATE, NULL, 'CC', Cust#);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)      
      values (v_OrderSEQ, Prod1#, Quan1);
    COMMIT;
    INSERT INTO OrderDetails (OrderID, ProductID, Quantity)    
      values (v_OrderSEQ, Prod2#, Quan2);
    COMMIT;
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select v_OrderSEQ, SYSDATE, Cust#, P.Price * Quan1 from Product P where P.ProductID = Prod1#;
    COMMIT;
    INSERT INTO OrderLog (Order#, OrderDateTime, Cust#, OrderTotalPrice)
      select v_OrderSEQ, SYSDATE, Cust#, P.Price * Quan2 from Product P where P.ProductID = Prod2#;
    COMMIT;
  END;  
END;
/
show errors
- read my previous comment. and check your original syntax what should be there near the begin.
Avatar of crazy4s

ASKER

okay package body created. but the all the execute doneworders still giving the errors...
- is it the same errors on OrderDetails? check does the data inserted into which tables?
Avatar of crazy4s

ASKER

i used the same way as previous
insert into Orderetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 10011, 10);

Error at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated -\
parent key not found
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crazy4s

ASKER

>>this is a new error. please verify the first error is on ORDER_FK_ORDERDETAILS and that is solved. and i've also solved the error on the foreignkey error. i hope you can open a new question on the new one.

I mentioned before IF I INSERT A VALID PRODUCTID
insert into OrderDetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 10011, 10);

Error at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.ORDER_FK_ORDERDETAILS) violated -
parent key not found

IF I INSERT AN INVALID PRODUCTID
insert into OrderDetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 99999, 10);

Error at line 1:
ORA-02291: integrity constraint (JESSICAFOO9.PRODUCT_FK_ORDERDETAILS) violated -
parent key not found

and i tried both...it gave the errors...
- check carefully the data that you are supplying to the package. in your table you have a 'check' on range of id that is accepted. see if you supplying the id within the correct range.
- i'm about to heading home, i'll continue once i reached home if other expert did not pickup from here. on the mean time, check the value of the sequence, verify the "checks" of id range.
Avatar of crazy4s

ASKER

yes the both check is within the range where the productid is 5 digit and orderid is 4 digit...so both are within the range....
- does the value exist in the parent table that it reference to?
Avatar of crazy4s

ASKER

for this below, YES, the productID is already exists in the Product table
insert into OrderDetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 10011, 10);

but for this below, NO, this is an invalid productID which doesn't exists in the Product table
insert into OrderDetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 99999, 10);
CREATE TABLE OrderDetails (

  OrderID INTEGER
    check (OrderID > 1000 and OrderID < 10000)
    CONSTRAINT Order_fk_OrderDetails
    REFERENCES ProductOrder(OrderID),

  ProductID INTEGER
    check (ProductID > 10000 and ProductID < 100000)
    CONSTRAINT Product_fk_OrderDetails
    REFERENCES Product(ProductID),
  Quantity INTEGER NOT NULL,
  CONSTRAINT OrderDetails_pk PRIMARY KEY (OrderID, ProductID)
);

- i think you haven't fully understand the usage of both foreignkey and parent-child concept in the table OrderDetails that you've created.

- now lets execute the package using the following value:
execute OrderProcessingDef.Do1Order(101, <use productid that exist in Product table>, 10, <use productid that exist in Product table>,10,'CASH');

- paste me any error that it produce.
"for this below, YES, the productID is already exists in the Product table
insert into OrderDetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 10011, 10);

but for this below, NO, this is an invalid productID which doesn't exists in the Product table
insert into OrderDetails (OrderID, ProductID, Quantity) values (OrderSEQ.nextval, 99999, 10);"
 
- if you want to test your insert statement outside the package, you should supply as following:
insert into OrderDetails (OrderID, ProductID, Quantity)
VALUES (<orderid that exist in ProductOrder>, <productid that exist in Product>, 10);
Avatar of crazy4s

ASKER

if i tried execute OrderProcessingDef.Do1Order(101, 10011, 10, 10012,10,'CASH');

Error at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDER#_PK_ORDERLOG) violated -
ORA-06512: at "JESSICAFOO9.ORDERPROCESSINGDEF", line 25
ORA-06512: at line 1
- yes we almost there now, hopefully. the orderdetails error is now solved i assume?
- look at the error. its a new error. check which constraint it referring to? check the table and column of the said constraint - the "parent" and the "child" table.
Avatar of crazy4s

ASKER

when i tried this
insert into OrderDetails (OrderID, ProductID, Quantity)
VALUES (1001, 10011, 10);

ERROR at line 1:
ORA-00001: unique constraint (JESSICAFOO9.ORDERDETAILS_PK) violated

i don't really get it why i keep violating the primary key... is there any mistakes i did when i create the table or anywhere?
Avatar of crazy4s

ASKER

the orderlog table...well i but i didn't see anyting wrong??? or did i missed out anything??

CREATE TABLE OrderLog (
  Order# INTEGER
    check (Order# > 1000 and Order# < 10000)
    CONSTRAINT Order#_pk_OrderLog PRIMARY KEY,
  OrderDateTime DATE,
  Cust# INTEGER
    check (Cust# > 100 and Cust# < 1000)
    CONSTRAINT Customer_fk_OrderLog
    REFERENCES Customer(CustomerID),
  OrderTotalPrice INTEGER
);
- hold on ID: 35981532 first. lets concentrate on the package itself in ID: 35981497.
- try understand the error of "unique constraint". what do understand on unique constraint concept?
"what do understand on unique constraint concept? "
- what i mean is based on the constraint name of the said error, check the table and what is defined there?
Avatar of crazy4s

ASKER

that means that can only have one no repetition??
- yes true! what is the purpose of the OrderLog  table? the question you need to consider is should we restrict that column can only have 1 value and cannot have repetition value?
Avatar of crazy4s

ASKER

yes what i think was the order# should be unique and not being repeated that's why we've the OrderSEQ for that to generate a different number each time right?
- it depends on your requirement for the table. if the table need to insert a same value to the column, you should not set it as primary key.

- basically the design of your table is another area that you want to look into on another question. lets correct the error on the current design that you have now.
"yes what i think was the order# should be unique and not being repeated that's why we've the OrderSEQ for that to generate a different number each time right?"

- you need to open a new question on your table design however i'm assisting based on your current design so that the package to work - for now:
: you have a master order table of ProductOrder and you have set the OrderID to be unique. so this is ok.
: then you have a detail order table of OrderDetails and the OrderID cannot be unique because in the same order can have different product and amount. this is fine too, for now.
: next is the OrderLog for logging/history purposes i assume. so the OrderID too cannot be unique because it should keeping all order logs.

Avatar of crazy4s

ASKER

sorry for the delay... i'm able to solve the unique constraint but i'm still stuck for the integrity constraints:(
- hi, thought you have settled this since not getting back to it.
- basically the integrity error is due to value you are inserting does not exist in the parent table. check the value you are passing to the child table in the referencing column, does the value exist in the column in the parent table?
Avatar of crazy4s

ASKER

no these value doesn't exist in the parent table...that's the reason i said it was either an invalid customer# or invalid product#....hence these order need to be put into the ErrorLog table(the error msg)....but my exception in the package seems to be not working as i didn't get any output for it....

well cos i was having my finals...so i just put this aside first:)
- ok i believe the procedure you have right now is already working fine. and to meet your homework requirement on trapping error, we can continue back to the other questions thread. see you there :)

OP