Solved

Oracle Trigger

Posted on 2014-02-15
7
661 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best datatype for oracle table email creation 8 108
pivot rows to columns 1 61
Oracle Errors 11 89
Excess Redo 3 49
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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…

752 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