简体   繁体   中英

Oracle SQL Trigger error: ORA-00933

So I'm trying to create a trigger that alters a records timestamp, I have this so far

    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;

Here is the description of the errors: Compilation failed, line 17 (16:54:27) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers. PL/SQL: ORA-00933: SQL command not properly endedCompilation failed, line 15 (16:54:27) The line numbers associated with compilation errors are relative to the first BEGIN statement. This only affects the compilation of database triggers. PL/SQL: SQL Statement ignored

By the sounds of it, it doesn't think my if statement has been closed properly, but I have no idea where I've gone wrong

When you are modifying the table the trigger is defined on, you want a before update trigger. So, something like this:

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;

Instead of:

    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;   

it's:

    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;   

Ie there is a comma instead of the second SET keyword.

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