我收到的SQL错误对我来说毫无意义。我使用以下命令创建了一个名为SUBORDER的表:
CREATE TABLE SUBORDER (
OrderNo int,
SuborderNo int,
ReqShipDate date,
ActualShipDate date,
BranchName varchar(50),
primary key (OrderNo, SuborderNo),
foreign key (OrderNo) references ORDERS(OrderNo) on delete set null,
foreign key (BranchName) references BRANCH(BranchName) on delete set null
);
如您所见,ReqShipDate和ActualShipDate被声明为日期。但是当我运行这段代码时:
create or replace trigger shipment_late
before update
on SUBORDER
for each row
declare
reqdate DATE;
actualdate DATE;
begin
select s.ReqShipDate, s.ActualShipDate into reqdate, actualdate
from SUBORDER s
where ( s.OrderNo = :new.OrderNo ) and ( s.SuborderNo = :new.SuborderNo );
if reqdate > actualdate then
insert into LATE_SHIPMENT
select * from SUBORDER s
where ( s.OrderNo = :new.OrderNo ) and ( s.SuborderNo = :new.SuborderNo );
end if;
end;
SQL控制台告诉我:
错误(12,11):PL / SQL:ORA-00932:不一致的数据类型:预期 NUMBER得到了DATE
为什么期待一个数字?
答案 0 :(得分:0)
看起来你只想要
create or replace trigger shipment_late
before update
on SUBORDER
for each row
begin
if :new.ReqShipDate > :new.ActualShipDate
then
insert into LATE_SHIPMENT( <<list of columns>> )
values( :new.OrderNo, :new.SuborderNo, ... );
end if;
end;
要注意的几件事
insert
语句,那么明确列出列是一个好主意。否则,您依赖的隐含订单对于阅读您的代码的人来说并不明显,并且会产生相当大的潜在错误。列出列,至少要记录您放在哪里的值。在此示例中,late_shipment
表似乎不太可能在suborder
中以完全相同的顺序定义完全相同的列集。SubOrder
)无法查询该表。这样做通常会引发变异表异常。但是,这似乎并不是必需的。您只需要引用:new
伪记录的各种属性。