我要创建一个触发器来修改记录的时间戳,目前我有这个
create or replace TRIGGER job_date_set
AFTER INSERT OR UPDATE OF start_date, closing_date ON jobs
FOR EACH ROW
BEGIN
IF UPDATING THEN
CASE
WHEN :OLD.closing_date != :NEW.closing_date THEN
UPDATE jobs
SET closing_date = to_date(to_date(:NEW.closing_date,'DD/MON/YYYY')||' 23:59:59','DD/MON/YYYY HH24:MI:SS')
WHERE :OLD.job_id = job_id;
WHEN :OLD.start_date != :NEW.start_date THEN
UPDATE jobs
SET start_date = to_date(to_date(:NEW.start_date,'DD/MON/YYYY') ||' 00:00:00','DD/MON/YYYY HH24:MI:SS')
WHERE :OLD.job_id = job_id;
END CASE;
END IF;
IF INSERTING THEN
UPDATE jobs
SET closing_date = to_date(to_date(:NEW.closing_date,'DD/MON/YYYY') ||' 23:59:59','DD/MON/YYYY HH24:MI:SS')
SET start_date = to_date(to_date(:NEW.start_date,'DD/MON/YYYY')||' 00:00:00','DD/MON/YYYY HH24:MI:SS')
WHERE :OLD.job_id = job_id;
END IF;
END;
错误描述如下:编译失败,第17行(16:54:27)与编译错误相关的行号相对于第一个BEGIN语句。这只会影响数据库触发器的编译。PL/SQL: ORA-00933: SQL command not proper enddcompilation failed, line 15(16:54:27)与编译错误相关的行号相对于第一个BEGIN语句。这只会影响数据库触发器的编译。PL/SQL: SQL Statement ignored
听起来,它不认为我的if语句已经正确关闭,但我不知道我哪里出错了
而不是:
UPDATE jobs
SET closing_date = to_date(to_date(:NEW.closing_date,'DD/MON/YYYY') ||' 23:59:59','DD/MON/YYYY HH24:MI:SS')
SET start_date = to_date(to_date(:NEW.start_date,'DD/MON/YYYY')||' 00:00:00','DD/MON/YYYY HH24:MI:SS')
WHERE :OLD.job_id = job_id;
:
UPDATE jobs
SET closing_date = to_date(to_date(:NEW.closing_date,'DD/MON/YYYY') ||' 23:59:59','DD/MON/YYYY HH24:MI:SS'),
start_date = to_date(to_date(:NEW.start_date,'DD/MON/YYYY')||' 00:00:00','DD/MON/YYYY HH24:MI:SS')
WHERE :OLD.job_id = job_id;
。用逗号代替第二个SET关键字
当您修改定义了触发器的表时,您希望在更新触发器之前有一个。像这样:
create or replace TRIGGER job_date_set
BEFORE INSERT OR UPDATE OF start_date, closing_date ON jobs
FOR EACH ROW
BEGIN
IF UPDATING THEN
CASE
WHEN :OLD.closing_date <> :NEW.closing_date THEN
SELECT to_date(to_date(:NEW.closing_date,'DD/MON/YYYY')||' 23:59:59','DD/MON/YYYY HH24:MI:SS')
INTO :NEW.closing_date
FROM dual;
WHEN :OLD.start_date <> :NEW.start_date THEN
SELECT to_date(to_date(:NEW.start_date,'DD/MON/YYYY') ||' 00:00:00','DD/MON/YYYY HH24:MI:SS')
INTO :NEW.start_date
FROM dual;
END CASE;
END IF;
IF INSERTING THEN
SELECT to_date(to_date(:NEW.closing_date,'DD/MON/YYYY') ||' 23:59:59','DD/MON/YYYY HH24:MI:SS'),
to_date(to_date(:NEW.start_date,'DD/MON/YYYY')||' 00:00:00','DD/MON/YYYY HH24:MI:SS')
INTO :NEW.closing_date, :NEW.start_date;
END IF;
END;