0

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

1
  • 1
    It looks like jobs.closing_date and jobs.start_date are date columns. If so, your to_date(to_date( should be to_date(to_char( - you want to convert the date into a character string in DD/MON/YYYY format in order to append ' 23:59:59' to it and convert it back to a date. (It might be simpler to trunc it, add 1 and subtract interval '1' second.) Commented Oct 31, 2016 at 11:49

2 Answers 2

1

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;   

I.e. there is a comma instead of the second SET keyword.

Sign up to request clarification or add additional context in comments.

Comments

1

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;

1 Comment

I think My plan was to have a trigger before insert that inserts an auto increment, so I would have to edit the date after, do you think It would be better to incorporate this code into that trigger or have two separate triggers doing two different tasks?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.