Link to home
Start Free TrialLog in
Avatar of poznan
poznan

asked on

Oracle triggers

I have 2 tables invoice and invoice item The invoice table contains Invoice_Number as primary key and invoice date and Paid_YN, the invoice_item table contains Invoice_Number as primary key and quantity_Sold, and item_Number as primary key also. I want to creat trigger  which will not allow records to be inserted/updated/deleted in the invoice item table if the corresponding invoice has been paid.

I tried this code which is not perfect
Create trigger TR_Not_allow
before insret_Update_delete
on Invoice_Item
for each row
begin
Select Paid_YN Into V-paid_YN
form invoice_Item
join Invoice
on Invoice_item.Invoice_Number = Invoice.Invoice_Number
if Paid_YN = 'True' then
Insert into Invoice_Item (  
Avatar of poznan
poznan

ASKER

Oracle triggers
ASKER CERTIFIED SOLUTION
Avatar of schwertner
schwertner
Flag of Antarctica 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

Perhaps this will work:

CREATE OR REPLACE TRIGGER TR_Not_allow
  BEFORE DELETE OR INSERT OR UPDATE ON Invoice_Item
  FOR EACH ROW
DECLARE
  V_Invoice_Paid VARCHAR2(1);
BEGIN
  SELECT Paid_Yn INTO V_Invoice_Paid
    FROM Invoice
   WHERE Invoice_Number = :New.Invoice_Number;

  IF V_Invoice_Paid = 'Y'  THEN
    Raise_Application_Error(-20201, 'Invoice Already Paid.';
  ENDIF;
END;
/

Open in new window

One of the the challenges you will face with a trigger to do this is the the fact that Oracle does not allow row-level triggers to either select from or modifiy any table related by foreign key, whether that is a parent table or a child table.  So, if these two tables have a foreign key defined (as they should, if you are using referential integrity) you won't be able to do this directly with a single database trigger.

There is a multi-step work around that you can use however.  This is just as effective, and almost as efficient as a single trigger, but it does involve a bit more coding.  You need to use three things:
1. A temporary holding place for each rowid (or primary key) . This can either be a global temporary table (for the simplest coding) or a PL\SQL table (for slightly faster performance).
2. A row-level trigger on the invoice_item trigger to save each rowid (or primary key) to the temporary holding place.
3. An "after statement" trigger on the invoice_item table that retrieves each rowid (or primary key) from the global temporary table or PL\SQL table, does the checking you want, and either returns an error (and rolls back the transaction) or does nothing (and allows the transaction to complete).
4. Depending on which device you use (a global temporary table or a PL\SQL table ) you may need to explicitly clear the records from the temporary holding place.  If you use a global temporary table with the default setting: "on commit delete rows", this will be done automatically for you.  If you use a PL\SQL table, you may need to clear it explicitly.

This approach works because "statement level" triggers in Oracle do not have the same restrictions regarding foreign-key related tables that "row level" triggers have.  But, "statement level" triggers have no idea which rows or how many rows were just changed, so a "row level" trigger is needed to put the row identifiers somewhere so the statement level trigger can find them.
Avatar of poznan

ASKER

Thanks