2

Created the following VendorAccount Table

VendorAccount

(Ven_AccountId, 
  Ven_RegNo,   
  Ven_Invoice_RefNo,    
  TotalAmount,
  Paid_ToVen ,    
  Balance
)

In the above table the value of TotalAmount is came from another Table(Vendor_InvoiceDetails).The TOTALBALANCE value is selected through POPUP LOV.

The Pad_TOVEN amount is subtracted from TOTALAMOUNT(TotalAMount-Pad_TOVEN).When ever i select TOTALAMOUNT it gives me the old TOTAL VALUE as shown below in picture. enter image description here

I want the updated the value.When ever i select TotalAmount it should give me the new updated value on the basis of Ven_Invoice_RefNo i-e 2300 in TotalAmount Column in new entry. Below is the trigger but it has the fallowing error

ERROR: PLS-00103: Encountered the symbol "DECLARE" when expecting one of the following: begin function pragma procedure subtype type <an identifier> <a double-quoted delimited-identifier> current curs

Trigger:

create or replace trigger "VENDORACCOUNT_T2"
BEFORE
insert or update or delete on "VENDORACCOUNT"
for each row
begin
DECLARE new_balance INT; 
DECLARE new_total INT;
DECLARE new_paid INT;

   SELECT balance INTO old_balance,
   total INTO old_total,
       PAID_TOVEN INTO new_paid
   FROM vendoraccount
   WHERE ven_regno = new.ven_regno
   AND VEN_INVOICE_REFNO = new.VEN_INVOICE_REFNO;

   UPDATE vendoraccount SET TOTALAMOUNT = old_total + old_balance - new_paid,
   balance = TOTALAMOUNT - new_paid
   WHERE VEN_REGNO= new.VEN_REGNO
   AND VEN_INVOICE_REFNO = new.VEN_INVOICE_REFNO;

end;
Usman YousafZai
  • 1,088
  • 4
  • 18
  • 44

2 Answers2

1

There are five things to be done as far as I see:

  1. Move begin below "declare" lines.
  2. Use just one "declare" keyword.
  3. Declare old_balance.
  4. Declare old_total.
  5. Just one "into".

    create or replace trigger "VENDORACCOUNT_T2"
    BEFORE
    insert or update or delete on "VENDORACCOUNT"
    for each row
    DECLARE new_balance INT; 
            new_total INT;
            new_paid INT;
            old_balance INT;
            old_total INT;
    begin
    
    SELECT balance, total, PAID_TOVEN 
    INTO old_balance, old_total, new_paid
    FROM vendoraccount
    WHERE ven_regno = new.ven_regno
    AND VEN_INVOICE_REFNO = new.VEN_INVOICE_REFNO;
    
    UPDATE vendoraccount SET TOTALAMOUNT = old_total + old_balance - new_paid,
    balance = TOTALAMOUNT - new_paid
    WHERE VEN_REGNO= new.VEN_REGNO
    AND VEN_INVOICE_REFNO = new.VEN_INVOICE_REFNO;
    end;
    
heuristican
  • 294
  • 1
  • 8
0

You should move the declaration section before 'begin'

Shepherdess
  • 651
  • 6
  • 7