2015-05-26 107 views
1

触发我的问题是:问题创建使用Oracle

触发这被称为“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' 
+2

首先,你的触发器是在SalesTransaction表上插入触发器之前,但您试图在触发器正文的select语句中加入SalesTransaction表。你肯定知道商定的价格和vin?他们是:new.agreedprice和:new.vin。接下来,SalesAgent是否真的与select中的其他表相关?我想你可能需要把这张表放到自己的select语句中。最后,如果你没有从你的选择中得到回应,你会很好地考虑会发生什么。它应该错误吗?你应该使用默认值吗?还有别的吗? – Boneist

回答

0

这个错误意味着你有“变成了” select语句条款产生任何行。 为了避免这个错误,我通常在每一列上使用聚合函数MAX(),它提供了1行结果集,在'没有匹配行'的情况下生成空值,并且没有异常增加