0

I have two tables:

Assessment and Assessment_Announcement

CREATE TABLE  "ASSESSMENT" 
  (     "ASSESSMENT_NAME" VARCHAR2(50) NOT NULL ENABLE, 
        "DEADLINE_DATE" DATE NOT NULL ENABLE, 
        CONSTRAINT "ASSESSMENT_PK" PRIMARY KEY ("ASSESSMENT_NAME") ENABLE
  )

CREATE TABLE  "ASSESSMENT_ANNOUNCEMENT" 
   (    "ASSESSMENT_NAME" VARCHAR2(50) NOT NULL ENABLE, 
        "DEADLINE_DATE" DATE NOT NULL ENABLE, 
        "ATTENTION" VARCHAR2(500) NOT NULL ENABLE, 
  CONSTRAINT "ASSESSMENT_ANNOUNCEMENT_PK" PRIMARY KEY ("ASSESSMENT_NAME")  ENABLE
   )

I am looking at implementing a trigger which updates to the 'ASSESSMENT_ANNOUNCEMENT' table when the date in the DEADLINE_DATE column on the ASSESSMENT table is within 7 days. The data might have been stored for 14 days, but when it is within 7 days of the deadline - it triggers. This shouldn't trigger on insert unless the insert date is within 7 days of the deadline.

So far I have the following code:

CREATE OR REPLACE TRIGGER  "TEST" 
AFTER INSERT ON ASSESSMENT
FOR EACH ROW
BEGIN
insert into ASSESSMENT_ANNOUNCEMENT(ASSESSMENT_NAME, DEADLINE_DATE, ATTENTION)
values (:new.ASSESSMENT_NAME, :new.DEADLINE_DATE, 'DEADLINE IS 7 DAYS OR LESS');
WHERE DEADLINE_DATE >= (SYSDATE) - 7

Any help and guidance would be greatly appreciated!

Thanks!

1
  • What would be the problem with your existing code? Feb 26, 2013 at 20:57

2 Answers 2

0

It sounds like you want

BEGIN
  IF :new.deadline_date >= sysdate + 7
  THEN
    INSERT INTO assessment_announcement( assessment_name, deadline_date, attention )
      VALUES( :new.assessment_name, :new.deadline_date, 'Deadline is 7 days or less' );
  END IF;
END;

Note that an Oracle DATE always contains a day component and a time component. So sysdate + 7 returns exactly 24*7 = 168 hours ago. If it is currently 4:00 PM on Feb 26, it would return 4:00 PM on March 5. If you want the trigger to insert a row in assessment_announcement if the deadline_date is any time on March 5, you'd need trunc(sysdate) + 7.

1
  • Hi guys! The help so far is great and I'm looking to have a go at getting some code to work! :) One point - If the assessment deadline has been entered one month ago and turns into it needing to trigger an update because the date has turned into it being due in 7 days - this isn't a true trigger is it? Because the user isn't inserting 7 days from the deadline - But I'm still looking to to trigger? I can't see any other way of updating across. Sorry for any confusion. And thanks again! Feb 26, 2013 at 21:21
0

I don't believe you can accomplish what you want with a trigger as you have no guarantee that the table will be updated within the timeframe you want. Instead you should be looking to run a stored procedure to do this that can be scheduled on an appropriate interval; say every day or every 12 hours. I've done this with MSSQL and not oracle, but this article should help you get started on your way.

The PL-SQL BLock you want to schedule is your query above. You could modify it to be

insert into ASSESSMENT_ANNOUNCEMENT(ASSESSMENT_NAME, DEADLINE_DATE, ATTENTION)
Select ASSESSMENT_NAME, DEADLINE_DATE, 'Deadline is 7 days or less'
FROM ASSESSMENT
WHERE DEADLINE_DATE >= (SYSDATE) - 7

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.