Triggers: how can I initialize the value on a tabl

2019-08-04 12:09发布

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条回答
趁早两清
2楼-- · 2019-08-04 12:58

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

查看更多
登录 后发表回答