更新列

时间:2012-12-06 12:03:44

标签: oracle oracle10g

我创建了一个包含以下属性的表:

 TABLE "VENDORACCOUNT" 

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

我根据TOTALAMOUNT值从另一个表中通过POPUP LOVVEN_INVOICE_REFNO列值。此处的方案是从TOTALAMOUNT列值中减去PAID_TOVEN列值。但是下次我选择TOTALAMOUNT值时,它不会显示更新的值。它显示了我在下面的报告中显示的旧值。

报告查询:

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

report screenshot

在上面的报告中,我希望每当我进行第二次输入时,它应该向我显示减去或更新的值,即1800而不是28004550而不是9550 } 分别。因此,下次我可以从18004550中减去金额。


我创建了这个触发器

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”   以下内容:begin function pragma procedure子类型current curs'

1 个答案:

答案 0 :(得分:1)

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

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);

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