简体   繁体   English

适用于View的Oracle SQL触发器

[英]Oracle SQL Trigger for View

I am having trouble with an SQL Trigger on a View. 我在View上遇到SQL触发器问题。 I have a table called Absence like so: 我有一张名为Absence的表,如下所示:

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 eg Eek! 我想创建一个触发器,当一名员工缺席天数超过20时,向DBMS发送一条消息,例如Eek! This staff is taking too much sick leave. 这名工作人员正在休病假。 In reality, it would probably be checked against a period of time eg in the last 6 months but this doesn't need to be that complex. 实际上,它可能会在一段时间内进行检查,例如在过去的6个月内,但这不需要那么复杂。 Simply when the total of periods of absence is above 20 days on insert of a new record into the Absence table. 简单地说,当将新记录插入缺勤表时,缺席期总数超过20天。

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. 如果消息可以打印出刚违反> 20天缺席规则的staff_id_fk,那将是一个奖励。

PS 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! PS我是一名大学生,虽然这可能以其他方式实施,但我们被要求尝试为我们的数据库场景创建触发器!

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. 当你最终解决问题时我感觉很棒,而且我通过在你的帮助下探索这个问题我已经学到了很多关于触发器的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM