触发器:如何初始化一个表的值和另一个表的值? [英] Triggers: how can I initialize the value on a table with a value on another?

查看:114
本文介绍了触发器:如何初始化一个表的值和另一个表的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个触发器,以使用表MODELO上的capacidad值初始化表VUELO上的plazas_disponibles值.像这样:

I'm creating a trigger to initialize the value plazas_disponibles on table VUELO with the value capacidad on table MODELO. Like this:

create or replace
TRIGGER inicializar_plazas_disponibles
    BEFORE INSERT OR UPDATE ON VUELO 
    BEGIN
         SET VUELO (plazas_disponibles) = MODELO (capacidad);
    END inicializar_plazas_disponibles; 

我遇到以下错误:

Error(2,10): PL/SQL: SQL Statement ignored
Error(2,14): PL/SQL: ORA-00922: missing or invalid option
Error(2,23): PL/SQL: ORA-00971: missing SET keyword

为什么?

推荐答案

因为这不是更改插入行中的值的方法-您需要使用:NEW语法(

Because that isn't how you change a value in the row being inserted - you need to modify it using the :NEW syntax (documentation); and you haven't shown how to retrieve a relevant value from the MODELO table.

您需要执行以下操作:

CREATE OR REPLACE TRIGGER inicializar_plazas_disponibles
BEFORE INSERT OR UPDATE ON vuelo
FOR EACH ROW
BEGIN
    SELECT capacidad
    INTO :NEW.plazas_disponibles
    FROM modelo
    WHERE ... some condition, presumably another :NEW column ...
END;

(尽管我不确定是否可以直接选择一个:NEW值-请尝试尝试该方法,但是如果不能,则需要声明相同类型的变量,而是选择一个相同类型的变量,然后将其分配给:NEW).

(Although I'm not entirely sure whether you can select straight into a :NEW value - try that, but if not you'll need to declare a variable of the same type, select into that instead, and then assign that to the :NEW).

这篇关于触发器:如何初始化一个表的值和另一个表的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆