我希望从Doctor
和Department
表中获取规范,以确保部门经理在插入时具有相同的规范..
create or replace TRIGGER check_dept_man
After Insert on DEPARTMENT
FOR EACH ROW
DECLARE
spec Varchar2(30);
dept_name Varchar2(30);
BEGIN
dept_name := :new.DEPT_NAME;
SELECT DR_SPECIALISATION INTO spec from DOCTOR where DR_ID= :new.MANAGER_ID;
IF ( dept_name != spec ) THEN
raise_application_error(-20353,"error man");
END IF;
END;
这是我得到的错误:
这是部门表:
CREATE TABLE "DEPARTMENT"
( "DEPT_ID" NUMBER(3,0),
"DEPT_NAME" VARCHAR2(30),
"MANAGER_ID" NUMBER(3,0),
"MANAGE_DATE" DATE,
"LOCATION" VARCHAR2(6),
"NUMBER_OF_DOCTORS" NUMBER(3,0),
"NUMBER_OF_NURSES" NUMBER(3,0),
CONSTRAINT "DEPT_ID_PK" PRIMARY KEY ("DEPT_ID") ENABLE,
CONSTRAINT "DEPT_NAME_UQ" UNIQUE ("DEPT_NAME") ENABLE
) ;
这是医生:
CREATE TABLE "DOCTOR"
( "DR_ID" NUMBER(5,0),
"DR_NAME" "FULL_NAME" ,
"DR_GENDER" VARCHAR2(6),
"DR_SPECIALISATION" VARCHAR2(30),
"DR_LOCATION" "LOCATION_TY" ,
"DEPT_NO" NUMBER(3,0),
"TOTAL_SURGERY_TIME" NUMBER(4,0),
CONSTRAINT "DR_GENDER_IS" CHECK (dr_gender IN ('Male','Female')) ENABLE,
CONSTRAINT "DR_ID_PK" PRIMARY KEY ("DR_ID") ENABLE
) ;
ALTER TABLE "DOCTOR" ADD CONSTRAINT "DR_DEPT_FK" FOREIGN KEY ("DEPT_NO")
REFERENCES "DEPARTMENT" ("DEPT_ID")
整件事就是这个'而且这......我花了一整天的时间来处理这个愚蠢的错误..谢谢@kordirko和你们所有的人
答案 0 :(得分:0)
感谢@kordirko
观察,它应该有效:
CREATE OR REPLACE TRIGGER check_dept_man
BEFORE INSERT ON DEPARTMENT
FOR EACH ROW
DECLARE
spec Varchar2(30);
dept_name Varchar2(30);
BEGIN
dept_name := :new.DEPT_NAME;
SELECT DR_SPECIALISATION INTO spec from DOCTOR where DR_ID= :new.MANAGER_ID;
IF dept_name != spec THEN
raise_application_error(-20353, 'error man');
END IF;
END;
/