Triggers: how can I initialize the value on a table with a value on another?

人走茶凉 提交于 2020-01-05 17:30:07

问题


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; 

And I'm getting the following errors:

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

Why?


回答1:


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.

You need to do something like:

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;

(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).



来源:https://stackoverflow.com/questions/4707979/triggers-how-can-i-initialize-the-value-on-a-table-with-a-value-on-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!