0

I am having trouble with an SQL Trigger on a View. I have a table called Absence like so:

CREATE TABLE Absence
(
 absence_id_pk varchar(6) NOT NULL,
 staff_id_fk varchar(6),
 start_date date,
 end_date date,
 reason varchar(30),
 PRIMARY KEY (absence_id_pk),
 FOREIGN KEY (staff_id_fk) REFERENCES Full_Time_Employee(staff_id_fk)
);

which records periods of staff absence from work.

Here is the problem! I would like to create a trigger that sends a message to the DBMS when a member of staff's total number of absent days is greater than 20 e.g. Eek! This staff is taking too much sick leave. In reality, it would probably be checked against a period of time e.g. in the last 6 months but this doesn't need to be that complex. Simply when the total of periods of absence is above 20 days on insert of a new record into the Absence table.

After reading some of the comments I have made this new trigger:

create or replace TRIGGER absence_check
BEFORE INSERT
ON absence
FOR EACH ROW
DECLARE
staffid absence.staff_id_fk%TYPE;
days number;
BEGIN
SELECT SUM(end_date - start_date) INTO days
FROM absence
WHERE staff_id_fk = staffid;
IF days > 20
THEN
DBMS_OUTPUT.PUT_LINE('Total days absent are more than 20' || staffid);
END IF;
END;

Any advice/guidance/solutions would be greatly appreciated! It would be a bonus if the message could print out the staff_id_fk that has just violated the > 20 days absent rule.

P.S. I am a University student and although this may be implemented in other ways, we have been asked to try and create triggers for our database scenario!

12
  • Create an insert trigger on the table absence. Why are you inserting into the view in this case? Feb 28, 2015 at 12:58
  • 3
    You seemed to have this working yesterday and you've now made it much complicated than in your comment? You're assuming that who/whatever does the insert is doing anything with the dbms_output buffer, which often isn't the case. And that whoever inserts does something with the info, and can't insert into the base table. A scheduled (monthly?) job that checks all employees and, say, emails HR might be more appropriate for this sort of thing?
    – Alex Poole
    Feb 28, 2015 at 13:46
  • P.S. The record can still be inserted if the absence is > 20 days. I just want the warning message output. Then the best thing to do is to create a scheduled job -- hourly, daily, monthly, or whatever -- not a trigger. Feb 28, 2015 at 14:59
  • 1
    Voting to close as a duplicate since this could have been an edit to your other question on the same topic. Feb 28, 2015 at 15:01
  • @DavidFaber You're right. I should have continued my thread from yesterday! >.< If this closes I will continue to search for an answer there. Also, I have no idea about scheduled jobs. I'm new to Triggers (we've only had 1 brief lecture on them) and I would like to get some Triggers working as an extra task for my University assignment. Feb 28, 2015 at 16:21

1 Answer 1

0
create or replace TRIGGER absence_check
BEFORE INSERT
ON absence
FOR EACH ROW
DECLARE
staffid absence.staff_id_fk%TYPE := :NEW.staff_id_fk;
days number;
BEGIN
SELECT SUM(end_date - start_date) INTO days
FROM Absence, Staff 
WHERE Absence.staff_id_fk = staffid;
IF days > 20
THEN
DBMS_OUTPUT.PUT_LINE('Warning: Total number of days absent is more than 20 for staff member: ' || staffid);
END IF;
END;

Thanks for everyone's help and suggestions. It feels great when you finally crack a problem and I have learnt so much about Triggers by exploring this problem with your help.

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.