Solved

Oracle triggers

Posted on 2011-02-10
5
656 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 47

Accepted Solution

by:
schwertner earned 500 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 34

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now