Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle Trigger

Posted on 2014-02-15
7
Medium Priority
?
670 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 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

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

Accepted Solution

by:
johnsone earned 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
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.
Suggested Courses

885 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