触发器包括if else语句

时间:2016-10-18 14:25:52

标签: sql oracle if-statement triggers

我已经把这个触发器的大部分工作了,我只是不知道如何正确地实现这个,如果其他的,我尝试评估各自的北,东等的长和纬度..等等。

CREATE OR REPLACE TRIGGER TR_SIGHTING_DESC
BEFORE INSERT ON sightings
FOR EACH ROW
DECLARE bn birds.bird_name%type;
DECLARE NS VARCHAR2(10), EW VARCHAR2(10);
BEGIN
IF :new.latitude >= 0 THEN
    EW := 'East'
ELSE
    EW := 'West'
END IF;

IF :new.longitude >= 0 THEN
    NS := 'North'
ELSE
    NS := 'South'
END IF;
SELECT bird_name
INTO bn
from birds
where bird_id = :new.bird_id;    
    :new.description := 'A bird of the species ' 
                        || bn 
                        || ' was spotted in the ' 
                        || EW|| '-'|| NS
                        || ' part of the observation area';
END;
/

INSERT INTO sightings (spotter_id, bird_id, latitude,
longitude, sighting_date)
VALUES (1024, 512, -25.6, 153, '09-MAR-2016');

对于if then语句和一般触发器的sql非常新。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

缺少逗号和两个DECLARE

CREATE OR REPLACE TRIGGER TR_SIGHTING_DESC
   BEFORE INSERT
   ON sightings
   FOR EACH ROW
DECLARE
   bn   birds.bird_name%TYPE;
   NS   VARCHAR2 (10);
   EW   VARCHAR2 (10);
BEGIN
   IF :new.latitude >= 0
   THEN
      EW := 'East';
   ELSE
      EW := 'West';
   END IF;

   IF :new.longitude >= 0
   THEN
      NS := 'North';
   ELSE
      NS := 'South';
   END IF;

   SELECT bird_name
     INTO bn
     FROM birds
    WHERE bird_id = :new.bird_id;

   :new.description :=
         'A bird of the species '
      || bn
      || ' was spotted in the '
      || EW
      || '-'
      || NS
      || ' part of the observation area';
END;
/