Solved

Oracle Trigger

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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 shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

867 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

25 Experts available now in Live!

Get 1:1 Help Now