PL / Sql查询执行错误

时间:2015-05-27 16:32:44

标签: oracle plsql triggers procedure

这是我的查询。触发器,自动存储在名为“ExcellentSale”的单独表中的销售代理 名称,汽车型号和制造商名称,每次商定的价格 SalesTransaction超过汽车要价的20%。 (注意:你需要创建 实现此触发器之前的'ExcellentSale'表。要创建主键,请使用 从1开始并以1为增量的序列。它显示像这样的错误

ERROR at line 23: PL/SQL: ORA-00942: table or view does not exist
1. create or replace trigger filltable
2. after insert on salestransaction
3. for each row
4. declare

触发器的代码是:

create or replace trigger filltable
after insert on salestransaction
for each row
declare
aname varchar2(25);
modname varchar2(25);
manfname varchar2(25);
askpr number;
agrpr number;
begin
select sa.name,m.name,mf.name
into   aname,modname,manfname
from   manufacturer mf,model m, car c, salestransaction st, salesagent sa
where mf.manufacturerid = m.manufacturerid and
m.modelno = c.modelno and
c.vin = st.vin and
st.agentid = sa.agentid;

select askingprice,agreedprice
into askpr,agrpr
from car c,salestransaction st
where c.VIN = St.vin;

if(agrpr > askpr*1.2) then

insert into excellentsales values(agent_seq.nextval,aname,modname,manfname);

end if;

end filltable;

/


create table excellentsales
(agentid varchar2(5) not null,
agentname varchar2(25),
carmodel varchar2(25),
mfname varchar2(25),
primary key(agentid))

CREATE SEQUENCE agent_seq START WITH 1 INCREMENT BY 1;

1 个答案:

答案 0 :(得分:0)

您不需要在SALESTRANSACTION的行触发器中查询SALESTRANSACTION - 您应该使用调用触发器的SALESTRANSACTION行上的:NEW值。我建议将你的触发器重写为:

create table excellentsales
(EXCELLENTSALES_ID  NUMBER
   CONSTRAINT PK_EXCELLENTSALES
   PRIMARY KEY
     USING INDEX,
agentid varchar2(5) not null,
agentname varchar2(25),
carmodel varchar2(25),
mfname varchar2(25))

CREATE SEQUENCE EXCELLENTSALES_SEQ
  START WITH 1
  INCREMENT BY 1;

create or replace trigger filltable
  after insert on salestransaction
  for each row
declare
  aname varchar2(25);
  modname varchar2(25);
  manfname varchar2(25);
  askpr number;
  agrpr number;
begin    
  SELECT m.NAME, mf.NAME, c.ASKINGPRICE
    INTO modname, manfname, askpr
    FROM CAR c
    INNER JOIN MODEL m
      ON m.MODELNO = c.MODELNO
    INNER JOIN MANUFACTURER mf
      ON mf.MANUFACTURERID = m.MANUFACTURERID
    WHERE c.VIN = :NEW.VIN;

  SELECT sa.NAME
    INTO aname
    FROM SALESAGENT sa
    WHERE sa.AGENTID = :NEW.AGENTID;

  agrpr := :NEW.AGREEDPRICE;

  if agrpr > askpr * 1.2 then
    insert into excellentsales (EXCELLENTSALES_ID, AGENTID, AGENTNAME, CARMODEL, MFNAME)
      values(EXCELLENTSALES_SEQ.nextval, :NEW.AGENTID, aname, modname, manfname);
  end if;
end filltable;

我还重新设计了您的EXCELLENTSALES表,以包含一个不是AGENTID的主键。最初设计的EXCELLENTSALES没有记录实际的AGENTID(显然应该来自SALESTRANSACTION),在现实世界中可能需要它,例如,支付佣金。