oracle Livesql触发器IF语句

68bkxrlz  于 2022-11-03  发布在  Oracle
关注(0)|答案(1)|浏览(166)

我需要假定每个雇员每个项目的工时上限为100小时。我的任务是开发一个触发器,以便在雇员超出项目的工时上限时跟踪加班工时。我需要有关触发器语句的帮助
创建表EMP_PROJ(雇员编号(4,0)非空,项目编号(4,0)非空,工时数(4,2)非空,约束EMP_PROJ_雇员编号_项目编号_PK主键(雇员编号,项目编号));

创建表(员工编号(4,0)不为空,项目编号(4,0)不为空,工时编号(22)不为空,约束员工_项目_加班时间_员工编号_项目编号_主键(员工编号,项目编号));

这是我目前掌握的情况。

CREATE OR REPLACE TRIGGER OVERTIME_HOURS
AFTER INSERT OR UPDATE OF hoursWorked ON EMP_PROJ
declare 
hoursWorked number(22);
BEGIN
        IF(hoursWorked > 100) 
        THEN
        INSERT INTO emp_proj_overtime(empNo, projNo, hourOt)
        SELECT empNo, projNo, hoursWorked - 100
        FROM EMP_PROJ;
        END IF;
END;

这个触发器的问题是,它没有将超过100的小时数发送到加班表。

qnzebej0

qnzebej01#

你不要select在一次触发;只需使用:new伪记录值。
示例表:

SQL> CREATE TABLE emp_proj (
  2      empno       NUMBER(4, 0) NOT NULL,
  3      projno      NUMBER(4, 0) NOT NULL,
  4      hoursworked NUMBER(6, 2) NOT NULL,  -- can't be NUMBER(4, 2) as you can't insert values => 100
  5      CONSTRAINT emp_proj_empno_projno_pk PRIMARY KEY ( empno,
  6                                                        projno )
  7  );

Table created.

SQL> CREATE TABLE emp_proj_overtime (
  2      empno  NUMBER(4, 0) NOT NULL,
  3      projno NUMBER(4, 0) NOT NULL,
  4      hourot NUMBER(22) NOT NULL,
  5      CONSTRAINT emp_proj_overtime_empno_projno_pk PRIMARY KEY ( empno,
  6                                                                 projno )
  7  );

Table created.

触发器:由于定义为组合关键字的主键约束条件,每个[empno,projno]组合只能有一行。

SQL> CREATE OR REPLACE TRIGGER overtime_hours AFTER
  2      INSERT OR UPDATE OF hoursworked ON emp_proj
  3      FOR EACH ROW
  4      WHEN ( new.hoursworked > 100 )
  5  BEGIN
  6      INSERT INTO emp_proj_overtime (
  7          empno,
  8          projno,
  9          hourot
 10      ) VALUES (
 11          :new.empno,
 12          :new.projno,
 13          :new.hoursworked - 100
 14      );
 15
 16  END;
 17  /

Trigger created.

测试(在更新现有行中的值期间出现加班小时数):

SQL> insert into emp_proj (empno, projno, hoursworked) values (1, 100, 75);

1 row created.

SQL> select * From emp_proj;

     EMPNO     PROJNO HOURSWORKED
---------- ---------- -----------
         1        100          75

SQL> select * From emp_proj_overtime;

no rows selected

SQL> update emp_proj set hoursworked = 102 where empno = 1 and projno = 100;

1 row updated.

SQL> select * From emp_proj;

     EMPNO     PROJNO HOURSWORKED
---------- ---------- -----------
         1        100         102

SQL> select * From emp_proj_overtime;

     EMPNO     PROJNO     HOUROT
---------- ---------- ----------
         1        100          2

更多测试(最初插入的加班时间):

SQL> insert into emp_proj (empno, projno, hoursworked) values (2, 995, 113);

1 row created.

SQL> select * From emp_proj;

     EMPNO     PROJNO HOURSWORKED
---------- ---------- -----------
         1        100         102
         2        995         113

SQL> select * From emp_proj_overtime;

     EMPNO     PROJNO     HOUROT
---------- ---------- ----------
         1        100          2
         2        995         13

SQL>

相关问题