问题使用Oracle创建触发器

时间:2015-05-26 10:11:50

标签: oracle plsql database-trigger

我的问题是:

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

我正在使用这些表

Manufacturer(manufacturerID, name, region)

Model(modelNo, name, type, previousModel, manufacturerID)

Car(VIN, dateAcquired, yearBuilt, purchasedPrice, askingPrice,
currentMileage, modelNo)

SalesAgent(agentID, name, DOB)

SalesTransaction(VIN, custID, agentID, dateOfSale, agreedPrice)

这是我的尝试

create sequence ggenerateKey
start with 1
increment by 1;
CREATE TABLE ExcellentSale(
recordNo NUMBER,
agentName VARCHAR2(20) NOT NULL,
modelName VARCHAR2(20) NOT NULL,
manufacturerName VARCHAR2(20) NOT NULL,
PRIMARY KEY(recordNo));
create or replace trigger AutoStore
before insert on SalesTransaction
for each row
declare
agentName varchar2(50);
modelName varchar2(50);
manufacturerName varchar2(50);
askingprice number;
agreedprice number;
begin
select sa.name, mo.name, mu.name, c.askingprice, st.agreedprice
into agentName, modelName, manufacturerName, askingprice, agreedprice
from manufacturer MU, Model MO, Car C, SalesAgent SA, SalesTransaction ST
where mu.manufacturerid = mo.manufacturerid
and st.vin = c.vin
AND c.vin = :new.vin
AND sa.agentID = :new.agentID;
IF :new.agreedPrice > (1.2 * askingPrice) THEN 
INSERT INTO ExcellentSale
VALUES
(ggenerateKey.nextval, agentName, modelName, manufacturerName);
END IF; 
end AutoStore;
/

触发器编译,当我尝试测试时,我使用这些将插入SalesTransaction的值,然后应该触发触发器,但显示为错误。

insert into SalesTransaction
values
('2B7JB33R9CK683376', '1', '1', to_date('01-02-2013','dd-mm-yyyy'), 586000 );

显示的错误是

insert into SalesTransaction
            *
ERROR at line 1:
ORA-01403: no data found 
ORA-06512: at "JTLA.AUTOSTORE", line 8 
ORA-04088: error during execution of trigger 'JTLA.AUTOSTORE' 

1 个答案:

答案 0 :(得分:0)

此错误表示您的select语句带有" into"子句不产生任何行。 为了避免这个错误,我通常在每一列上使用聚合函数MAX(),它使用1行结果集,在没有匹配行的情况下生成空值,并且没有异常上升