oracle sql触发尝试创建自动填充描述

时间:2016-10-18 10:38:52

标签: sql oracle triggers

我正在尝试创建一个触发器,当我:

INSERT INTO sightings (spotter_id, bird_id, latitude,
longitude, sighting_date)
    VALUES (2457, 901, -28.0, 152, '09-MAR-2016');

目击表中的描述将包含:

  

'在(X),(Y)部分发现了一种鸟类(BIRD-NAME)   观察区域

这就是我目前的情况:

CREATE OR REPLACE TRIGGER TR_SIGHTING_DESC
BEFORE INSERT ON sightings
 REFERENCING NEW ROW AS New
FOR EACH ROW
    SET New.description = CONCAT(CONCAT(CONCAT(‘A bird of the species ' bird_name ' was spotted in the ' latitude,
longitude ' part of the
observation area’
)));
END;
/

使用触发器的新手。我是在正确的轨道上吗?

2 个答案:

答案 0 :(得分:2)

您要插入的值可使用:new pseudorecord。 所以你的触发器将是:

CREATE OR REPLACE TRIGGER TR_SIGHTING_DESC
BEFORE INSERT ON sightings
FOR EACH ROW
BEGIN    
    :new.description := 'A bird of the species ' 
                        || :new.bird_name 
                        || ' was spotted in the ' 
                        || :new.latitude || ','|| :new.longitude 
                        || ' part of the observation area';
END;

请参阅本文档中有关关联名称和伪记录的部分。 https://docs.oracle.com/cd/E18283_01/appdev.112/e17126/triggers.htm

答案 1 :(得分:0)

这有用吗?

CREATE OR REPLACE TRIGGER TR_SIGHTING_DESC
BEFORE INSERT ON sightings
FOR EACH ROW
BEGIN    
    SELECT 'A bird of the species ' || bird_name || ' was spotted in the ' || latitude || longitude || ' part of the observation area'
    INTO :new.description
    FROM dual;
END;