-3

我必须在 Employee 表上创建一个触发器。如果为 Employee 表发出 INSERT 或 UPDATE 语句,触发器将启动并确保“salary”字段的值符合 job_min_sal 表中的条件。一遍又一遍地尝试后,我得到了一个变异表错误,现在非常沮丧,不知道该怎么办。

 JOB_MIN_SALARY TABLE:
  JOB VARCHAR2(50) PRIMARY KEY
  MIN_SAL NUMBER(7,2) NOT NULL

JOB_MIN_SAL 表填充了各种职位和薪水。我对使用触发器感到困惑,想知道是否可以从这里获得一些帮助

 CREATE OR REPLACE TRIGGER employee_job_salary
   BEFORE INSERT OR UPDATE OF SALARY on employee
     FOR EACH ROW
     DECLARE 
      v_salary    NUMBER;


    BEGIN
      SELECT minimum_salary
      INTO v_salary
      FROM job_min_salary
      WHERE UPPER(job) = UPPER(:NEW.job);

我知道我真的很遥远,我只是在寻求帮助,以了解这需要什么以及我需要采取哪些步骤来获得它。谢谢!

 The EMPLOYEE table:

 (
    EMPLOYEE_ID NUMBER(4)
    EMPLOYEE_NAME VARCHAR2(20)
    JOB VARCHAR2(50)
    MANAGER_ID NUMBER(4)
    HIRE_DATE DATE
    SALARY NUMBER(9)
    COMMISION NUMBER(9)
    DEPARTMENT_ID NUMBER(4)
 );
4

2 回答 2

0
CREATE TABLE job_min_salary
(
    job     VARCHAR2(50) PRIMARY KEY,
    min_sal NUMBER(7,2)  NOT NULL
);

INSERT INTO job_min_salary VALUES('CEO','100');
-- 1 rows inserted.

CREATE TABLE employee
(
    employee_id     NUMBER(4),
    employee_name   VARCHAR2(20),
    job             VARCHAR2(50),
    manager_id      NUMBER(4),
    hire_date       DATE,
    salary          NUMBER(9),
    commision       NUMBER(9),
    department_id   NUMBER(4)
);

INSERT INTO employee VALUES(1, 'Name', 'CEO', 1, TO_DATE('2000-01-01', 'YYYY-MM-DD'), 80, 80, 1);
-- 1 rows inserted.

CREATE OR REPLACE TRIGGER employee_job_salary
BEFORE INSERT OR UPDATE OF salary ON employee
FOR EACH ROW
DECLARE 
    v_salary  NUMBER(1);
BEGIN
    SELECT  1
    INTO    v_salary
    FROM    job_min_salary
    WHERE   UPPER(job)  = UPPER(:NEW.job)
    AND     :NEW.salary >= min_sal;
EXCEPTION
    WHEN NO_DATA_FOUND THEN
        RAISE_APPLICATION_ERROR(-20999, 'Salary value is too low for given job');
END;
-- TRIGGER EMPLOYEE_JOB_SALARY compiled

SELECT * FROM employee;
-- 1    Name    CEO 1   2000-01-01 00:00:00 80  80  1

UPDATE employee
SET    salary = 10
WHERE  job    = 'CEO';
-- ORA-20999: Salary value is too low for given job

UPDATE employee
SET    salary = 100
WHERE  job    = 'CEO';
-- 1 rows updated.

SELECT * FROM employee;
-- 1    Name    CEO 1   2000-01-01 00:00:00 100 80  1
于 2013-06-07T09:34:16.177 回答
0

我假设您正在做一些事情,例如将新工资与最低工资标准进行比较,并且仅在 :new.SALARY >= v_salary 时更新

如果不满足,你在做什么,你是捕获异常还是忽略错误或返回错误代码进行调试。

发布更多信息

于 2013-06-07T07:35:07.490 回答