如何使用 SQL 中的触发器更新基于其他两行的行
How to use a trigger in SQL to update a row, based in two other rows
也许它太简单了,但我对此任务感到头疼,我正在为一些家庭作业建立一个健身房数据库,我必须分别根据其他名为身高和体重的行来计算 BMI。我不知道更新这个的方法table,我已经试过了
create or replace trigger calculate_BMI
after insert or update on evaluation
begin
update evaluation
set BMI = i.bmi, weight = weight(height*height)
from inserted as i
end
这就是我发现的
SQL Error: ORA-04098: trigger 'BJ111237.CALCULATE_BMI' is invalid and failed re-validation
04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation"
*Cause: A trigger was attempted to be retrieved for execution and was
found to be invalid. This also means that compilation/authorization
failed for the trigger.
*Action: Options are to resolve the compilation/authorization errors,
disable the trigger, or drop the trigger.
它不起作用,如果有人带我去学习如何正确执行此操作,我将不胜感激,因为我被卡住了
假设您正在使用标准 BMI 计算并且您的值使用适当的单位存储,那么更新的 Oracle 语法将是:
create or replace trigger calculate_BMI
before insert or update on evaluation
for each row
begin
:new.bmi := :new.weight / (:new.height * :new.height);
end;
这是一个 "before" 触发器。它只是设置新值进行计算。不需要更新,因为您想更改正在更新的 table 的同一行中的列值。
Here 是一个正在展示它的 rextester。
您的代码有问题:
触发器无法对触发它的 table 进行操作;您确实想要一个设置 bmi
的 before
触发器,而不是尝试更新 table evaluation
的 after
触发器
您的触发器应该逐行触发,而不是在语句级别触发(因此它需要 for each row
选项)
伪-table inserted
在Oracle中不存在;相反,您可以使用 :new.
访问为更新或插入传递的值
你可能想要:
create or replace trigger calculate_bmi
after insert or update on evaluation
for each row
begin
:new.bmi := :new.weight / (:new.height * :new.height);
end
您需要指定是要更改现在更新的新值还是旧值。
例如:
set new.BMI = i.bmi, weight = new.weight(height*height)
也许它太简单了,但我对此任务感到头疼,我正在为一些家庭作业建立一个健身房数据库,我必须分别根据其他名为身高和体重的行来计算 BMI。我不知道更新这个的方法table,我已经试过了
create or replace trigger calculate_BMI
after insert or update on evaluation
begin
update evaluation
set BMI = i.bmi, weight = weight(height*height)
from inserted as i
end
这就是我发现的
SQL Error: ORA-04098: trigger 'BJ111237.CALCULATE_BMI' is invalid and failed re-validation
04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation"
*Cause: A trigger was attempted to be retrieved for execution and was
found to be invalid. This also means that compilation/authorization
failed for the trigger.
*Action: Options are to resolve the compilation/authorization errors,
disable the trigger, or drop the trigger.
它不起作用,如果有人带我去学习如何正确执行此操作,我将不胜感激,因为我被卡住了
假设您正在使用标准 BMI 计算并且您的值使用适当的单位存储,那么更新的 Oracle 语法将是:
create or replace trigger calculate_BMI
before insert or update on evaluation
for each row
begin
:new.bmi := :new.weight / (:new.height * :new.height);
end;
这是一个 "before" 触发器。它只是设置新值进行计算。不需要更新,因为您想更改正在更新的 table 的同一行中的列值。
Here 是一个正在展示它的 rextester。
您的代码有问题:
触发器无法对触发它的 table 进行操作;您确实想要一个设置
bmi
的before
触发器,而不是尝试更新 tableevaluation
的 您的触发器应该逐行触发,而不是在语句级别触发(因此它需要
for each row
选项)伪-table
inserted
在Oracle中不存在;相反,您可以使用:new.
访问为更新或插入传递的值
after
触发器
你可能想要:
create or replace trigger calculate_bmi
after insert or update on evaluation
for each row
begin
:new.bmi := :new.weight / (:new.height * :new.height);
end
您需要指定是要更改现在更新的新值还是旧值。
例如:
set new.BMI = i.bmi, weight = new.weight(height*height)