在PL / SQL程序中,我得到了Missing表达式错误,如下所示

时间:2017-03-17 07:30:13

标签: oracle plsql database-trigger ora-00936

CREATE OR REPLACE TRIGGER sales_trig1 
    before insert on Inventory1 
    for each row
DECLARE
    q Inventory1.qty%type; 
    id Inventory1.item_id%type;
    si Sales.sitem_id%type; 
    tid Sales.trans_id%type; 
    td Sales.trans_dt%type; 
    tq Sales.trans_qty%type;
BEGIN
    tid:='t_id111';  
    si:='&si';  
    tq:=&tq;
    select item_id,qty 
    into id,q 
    from Inventory1 
    where id=si; 
    IF tq<=q THEN 
       insert into Sales 
       values(tid,si,date,tq); 
       q:=q-tq; 
    END IF;    
EXCEPTION 
    WHEN no_data_found THEN 
         dbms_output.put_line('EXCEPTION: Invalid item_id');

END;

错误如下

Errors for TRIGGER SALES_TRIG1:
-
LINE/COL ERROR
-------- ----------------------------------------------------------------- 
17/1     PL/SQL: SQL Statement ignored
17/33   PL/SQL: ORA-00936: missing expression

1 个答案:

答案 0 :(得分:0)

如果您想从newly inserted rows获取和更改值,请使用:NEW.<column_name>来解决新值。

以下是它如何适用于您的案例:

CREATE OR REPLACE TRIGGER sales_trig1 
  before insert on Inventory1 
    for each row
DECLARE
  q Inventory1.qty%type; 
  id Inventory1.item_id%type;
  si Sales.sitem_id%type; 
  tid Sales.trans_id%type; 
  td Sales.trans_dt%type; 
  tq Sales.trans_qty%type;
BEGIN
  tid :='t_id111';  
  si  := :NEW.sytem_id;  
  tq  := :NEW.trans_dt;
  select item_id, qty into id, q from Inventory1 where id = si; 
  IF tq <= q THEN 
    -- insert in another table
    insert into Sales values(tid, si, sysdate, tq); 
    -- here change inserted value
    :NEW.qty := q - tq; 
  END IF;    
EXCEPTION 
  WHEN no_data_found THEN 
     dbms_output.put_line('EXCEPTION: Invalid item_id');

END;
/