Solved

Oracle Trigger

Posted on 2014-02-15
7
649 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
  • 4
  • 2
7 Comments
 
LVL 31

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 31

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
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.

 

Author Comment

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

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

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

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

17 Experts available now in Live!

Get 1:1 Help Now