Solved

Oracle Trigger

Posted on 2014-02-15
7
657 Views
Last Modified: 2014-02-16
I'm trying to update the values in one table from a trigger in another table. What am I doing wrong?

CREATE OR REPLACE TRIGGER TRG_MEM_BAL
AFTER UPDATE OF DETAIL_DUEDATE, DETAIL_RETURNDATE ON DETAILRENTAL

BEGIN

  UPDATE MEMBERSHIP
  SET MEMBERSHIP.MEM_BALANCE = DETAILRENTAL.DETAIL_DAILYLATEFEE * DETAILRENTAL.DETAIL_DAYSLATE;

END;
0
Comment
Question by:mfinocc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 39861603
Can you post a describe of the membership and detailrental tables?
0
 

Author Comment

by:mfinocc
ID: 39861612
MEMBERSHIP
MEM_NUM              NUMBER(8,0)
MEM_FNAME              VARCHAR2(30 BYTE)
MEM_LNAME              VARCHAR2(30 BYTE)
MEM_STREET              VARCHAR2(120 BYTE)
MEM_CITY              VARCHAR2(50 BYTE)
MEM_STATE              CHAR(2 BYTE)
MEM_ZIP                      CHAR(5 BYTE)
MEM_BALANCE      NUMBER(10,2)

DETAILRENTAL
RENT_NUM                      NUMBER(8,0)      
VID_NUM                              NUMBER(8,0)            
DETAIL_FEE                      NUMBER(5,2)            
DETAIL_DUEDATE              DATE            
DETAIL_RETURNDATE      DATE      
DETAIL_DAILYLATEFEE      NUMBER(5,2)            
DETAIL_DAYSLATE              NUMBER(3,0)
0
 
LVL 32

Expert Comment

by:awking00
ID: 39861622
There doesn't appear to be any relationship (i.e. no foreign keys) between the two tables, so how do you know which membership account needs an update of the balance?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mfinocc
ID: 39861679
Here's all the table information...
TinyVideo.sql
0
 
LVL 35

Accepted Solution

by:
johnsone earned 500 total points
ID: 39861969
You don't want to update every record in the MEMBERSHIP table, so you need a where clause.  Also, you do not reference the values in the trigger by using the table name.  You need to add to the balance, not set the balance.

So, in it's simplest form, I believe this is what you need.
CREATE OR replace TRIGGER trg_mem_bal 
  AFTER UPDATE OF detail_duedate, detail_returndate ON detailrental 
  FOR EACH ROW 
BEGIN 
    UPDATE membership 
    SET    membership.mem_balance = 
           Nvl(mem_balance, 0) + ( :new.detail_dailylatefee 
                                   * 
    :new.detail_dayslate ) 
    WHERE  mem_num = (SELECT mem_num 
                      FROM   rental 
                      WHERE  rent_num = :new.rent_num); 
END; 

/ 

Open in new window

I would question the additional requirements.  I'm sure there has to be more to it than this.
0
 

Author Closing Comment

by:mfinocc
ID: 39862556
Excellent!
0
 

Author Comment

by:mfinocc
ID: 39862562
I have posted one more question using this same database. Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

735 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