简体   繁体   中英

PL/SQL - Simple trigger, take from one column and IF-THEN-ELSE to another column

I am trying to figure out where I am going wrong on this simple trigger. I am quite new to triggers and trying to get used to using them with IFTTT statements.

I want the trigger to watch for a new row entry, and if the value is within a certain range within a column ( col_a ) it will then enter a certain value in the same row, but different column ( col_b ), which will be NULL up until this is entered. Please can you help?

CREATE TRIGGER trg_test
BEFORE INSERT
    ON test_table 
    FOR EACH ROW
BEGIN
    IF :new.col_a >= 10
    THEN :new.col_b := 'High';   
  ELSE
    :new.col_b := 'Low';
  END IF;
END;

It just keeps coming back with "success with compilation error".

You can easily do this using a virtual column:

alter table test_table
    add col_b generated always as (case when col_a >= 10 then 'High' else 'Low' end);

I wish that exercises in triggers used reasonable use-cases. This is not one -- you need both an insert and an update trigger, for instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM