我正在制作一个触发器,检查一个人是否可以去世界或欧洲锦标赛,如果他们开车去计算他们花在燃料上的费用(燃料成本*花费*距离/ 100)。我需要将该信息放在另一个表中,但我收到了这条消息:
Error(24,66): PL/SQL: ORA-00984: column not allowed here
那是因为我在代码底部的INSERT语句中加了“fuelcost *花费*:new.distance / 100”。我的问题是,我怎样才能以不同的方式将这个价值放在表格成本中。 Column costs.money不是计算列,因为它不仅仅是该表中的燃料成本,因此这不是解决方案。
CREATE OR REPLACE TRIGGER check_norm_fuel_cost
INSTEAD OF INSERT OR UPDATE ON insert_travel_view
FOR EACH ROW
DECLARE
hasnorm char(1);
spends number;
fuelcost number;
BEGIN
SELECT norms.norm INTO hasnorm
FROM norms JOIN competitions
ON competitions.competitionID=norms.competitionID
WHERE :new.personID = norms.personID
AND (competitions.name LIKE 'European championship'
OR competitions.name LIKE 'World championship');
IF hasnorm = 'Y' OR hasnorm IS NULL THEN
INSERT INTO travel(travelID, personID, competitionID, vehicleID, distance)
VALUES (:new.travelID, :new.personID, :new.competitionID, :new.vehicleID, :new.distance);
IF :new.distance IS NOT NULL THEN
SELECT vehicles.consumption INTO spends FROM vehicles
WHERE :new.vehicleID = vehicles.vehicleID;
SELECT fuels.cost INTO fuelcost FROM fuels JOIN vehicles ON vehicles.fuelID = fuels.fuelID
WHERE :new.vehicleID = vehicles.vehicleID;
INSERT INTO COSTS(costID, costtype, travelID, money)
VALUES(35, 'fuel', :new.travelID, fuelcost*spends*:new.distance/100);
END IF;
END IF;
END;
答案 0 :(得分:0)
尝试将insert语句中的显式值更改为 选择插入(...)选择....来自,抱歉从移动设备写错了格式。
答案 1 :(得分:0)
你似乎有三个问题; ORA-00984是因为您的最终插入正在执行:
VALUES(35, 'fuel', :new.travelID, fuelcost*spends*distance/100);
其中distance
被解释为列名,因为范围内没有该名称的变量;它可能应该是:
VALUES(35, 'fuel', :new.travelID, fuelcost*spends*:new.distance/100);
虽然您可以避开spends
和fuelcost
局部变量,而是执行insert ... select
:
INSERT INTO COSTS(costID, costtype, travelID, money)
SELECT 35, 'fuel', :new.travelID, fuels.cost*vehicles.consumption*:new.distance/100
FROM fuels JOIN vehicles ON vehicles.fuelID = fuels.fuelID
WHERE vehicles.vehicleID = :new.vehicleID;
你也有两个拼写错误:
INSERT INTO travel(travelID, personID, competitonID, vehicleID, distance)
缺少i
,应该(我认为)是:
INSERT INTO travel(travelID, personID, competitionID, vehicleID, distance)
和
WHERE :new.vehicleID = vehicle.vehicleID;
缺少s
,应该(我认为)是:
WHERE :new.vehicleID = vehicles.vehicleID;