Home » SQL & PL/SQL » SQL & PL/SQL » Re: want to stop the insert/delete/update transaction in an AFTER TRIGGER
Re: want to stop the insert/delete/update transaction in an AFTER TRIGGER [message #39975] Thu, 29 August 2002 12:22 Go to next message
Gaurang
Messages: 4
Registered: August 2002
Junior Member
consider this example:
there 3 tables customer(cust_no,c_name,balance)
deposit(cust_no,date,deposit_amount)
withdraw(cust_no,date,drawn_amount)
REQUIREMENTS: Balance should be minimum 5000
CAN YOU GIVE ME THE SOLUTION

problem is : trigger is created with compilation errors and i am not able to detect the errors in the following code.Please help me.

create trigger update_bal
before
insert or update of d_amount or delete on deposit
for each row
declare
bal customer.balance%type;
trans_error EXCEPTION;
begin
bal := :old.balance + :new.d_amount;
if bal >= 5000 then
if inserting then
update customer
set balance := bal
where cust_no = :new.cust_no;
elseif deleting then
update customer
set balance := balance - bal
where cust_no = :new.cust_no;
elseif updating then
update customer
set balance := bal
where cust_no = :new.cust_no;
else
RAISE trans_error;
end if;
end if;
Exeception
WHEN trans_error then
raise_application_error(-20026,'Transaction terminated as bal is < then 5000 ');
end;
Re: want to stop the insert/delete/update transaction in an AFTER TRIGGER [message #39977 is a reply to message #39975] Thu, 29 August 2002 14:37 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
1) I would think you would want both types of transactions in a single table with a transaction type (CR/DB). Separating tables by transaction type is making your queries and DML far more complicated than it should be.

2) There are many errors (syntatical and logical) with your trigger. Here is a suggested rewrite:

create or replace trigger update_bal
  before insert or update of d_amount or delete on deposit
  for each row
declare
  v_amount   customer.balance%type;
  v_cust_no  customer.cust_no%type;
  v_balance  customer.balance%type;
begin
  if inserting or updating then
 
    if inserting then
      v_amount := :new.d_amount;
    elsif updating then
      v_amount := :new.d_amount - :old.d_amount;
    end if;
 
    v_cust_no := :new.cust_no;
 
  elsif deleting then
 
    v_amount := -:old.d_amount;
    v_cust_no := :old.cust_no;
 
  end if;  
 
  update customer
     set balance := nvl(balance, 0) + v_amount
   where cust_no = v_cust_no
   returning balance into v_balance;
 
  if v_balance < 5000 then  
    raise_application_error(-20026, 'Transaction terminated as balance < 5000');
  end if;  
end;
/


Let me know any questions/comments you may have.
Re: want to stop the insert/delete/update transaction in an AFTER TRIGGER [message #39981 is a reply to message #39975] Fri, 30 August 2002 03:08 Go to previous messageGo to next message
ritesh kumar tiwary
Messages: 7
Registered: August 2002
Junior Member
alter trigger ABC disable
Re: want to stop the insert/delete/update transaction in an AFTER TRIGGER [message #39983 is a reply to message #39981] Fri, 30 August 2002 08:05 Go to previous message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Huh? What does this have to do with the question?
Previous Topic: Re: SID ?
Next Topic: Reg DECODE
Goto Forum:
  


Current Time: Thu Apr 25 04:52:35 CDT 2024