0

我创建了一个具有以下属性的表:

 TABLE "VENDORACCOUNT" 

 (  "VEN_ACCOUNTID" 
    "VEN_REGNO" 
    "VEN_TXDATE"
    "VEN_INVOICE_REFNO"
    "TOTALAMOUNT"
    "PAID_TOVEN"
    "BALANCE"
)

我根据其值从另一个表中获取TOTALAMOUNT列值。这里的场景是从列值中减去列值。但是下次我选择该值时,它不会显示更新的值。它向我显示了旧值,如下面的报告所示。 POPUP LOVVEN_INVOICE_REFNOTOTALAMOUNTPAID_TOVENTOTALAMOUNT

报告查询:

select "VEN_ACCOUNTID",
      "VEN_REGNO" ,
      "VEN_TXDATE" ,
      "VEN_INVOICE_REFNO" as ,
      "TOTALAMOUNT" as ,
      "PAID_TOVEN" as ,
       TOTALAMOUNT-PAID_TOVEN as "Balance"
 from    "VENDORACCOUNT" 

报告截图

在上面的报告中,我希望每当我做第二个条目时,它应该向我显示减去或更新的值,即分别1800代替28004550代替9550。所以下次我可以从1800和中减去金额4550


我创建了这个触发器

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;

并收到此错误:

错误:PLS-00103:'在预期以下情况之一时遇到符号“DECLARE”:开始函数杂注过程子类型类型当前 curs'

4

1 回答 1

1

我认为你不需要触发器。您可以根据以下查询创建视图,然后根据VIEW. 查询看起来像 -

select accid,regno,inv_refno,LAG(bal,1,totalamount) OVER (PARTITION BY regno ORDER BY accid) "TOTALAMOUNT", paid_toven, bal
from (with temp_data as
       (select 2 accid,3 regno, 16 inv_refno, 2800 totalamount, 1000 paid_toven from dual
         union
        select 3 accid,3 regno, 16 inv_refno, 2800 totalamount, 2000 paid_toven from dual
         union
        select 4 accid,8 regno, 22 inv_refno, 9550 totalamount, 5000 paid_toven from dual
         union
        select 5 accid,8 regno, 22 inv_refno, 9550 totalamount, 5000 paid_toven from dual
         union
        select 6 accid,8 regno, 22 inv_refno, 9550 totalamount, 8000 paid_toven from dual)
      select accid,regno,inv_refno,totalamount,paid_toven,totalamount-paid_toven bal       
      from temp_data);

输出是 -

ACCID REGNO INV_REFNO TOTALAMOUNT PAID_TOVEN BAL
----- ----- --------- ----------- ---------- ---
    2     3        16        2800       1000 1800 
    3     3        16        1800       2000 800 
    4     8        22        9550       5000 4550 
    5     8        22        4550       5000 4550 
    6     8        22        4550       8000 1550 

因此,根据您的表格,查询将是-

select accid,regno,inv_refno,LAG(bal,1,totalamount) OVER (PARTITION BY regno ORDER BY accid) "TOTALAMOUNT", paid_toven, bal
    from (select accid,regno,inv_refno,totalamount,paid_toven,totalamount-paid_toven bal       
          from VENDORACCOUNT);

该查询几乎可以满足您的需求,但是您的示例数据看起来不正确。这实际上是运行总计的一个示例。

于 2012-12-06T15:44:18.353 回答