Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Oracle triggers

Posted on 2011-02-10
5
Medium Priority
?
677 Views
Last Modified: 2012-05-11
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 (  
0
Comment
Question by:poznan
5 Comments
 

Author Comment

by:poznan
ID: 34868361
Oracle triggers
0
 
LVL 48

Accepted Solution

by:
schwertner earned 2000 total points
ID: 34869909

With some modification needed because I do not know:

1. How the tables are joined (which is the Foreign Key)
2. How to know that the order is paid

you can use this:

CREATE OR REPLACE TRIGGER TR_Not_allow
  BEFORE DELETE OR INSERT OR UPDATE ON invoiceitem
  FOR EACH ROW
DECLARE
    v_invoice_paid VARCHAR2(1);
BEGIN
     SELECT invoice.paid_column INTO  v_invoice_paid
     FROM   invoice, invoice item
     WHERE  invoice.Invoice_Number = :new.invoice_number;
     

     IF v_invoice_paid <> 'P'  THEN
           Raise_application_error(-20201, 'Not paid';
     ENDIF;
END;
/
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 34872555

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

0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 34874256
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.
0
 

Author Comment

by:poznan
ID: 34902902
Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question