1

我有这个虚拟类型:

create or replace type Service_TY as object(
  code INTEGER,
  visit_analysis char(1)
)FINAL;
/
create or replace type Employee_TY as object(
   dummy varchar(30)
)NOT FINAL;
/

create or replace type Doctor_TY UNDER Employee_TY(
  ID INTEGER
)FINAL;
/

create or replace type Assistant_TY UNDER Employee_TY(
  ID INTEGER
)FINAL;
/

create or replace type Habilitation_TY as object(
  employee ref Employee_TY,
  service ref Service_TY
)FINAL;
/

还有这些虚拟表:

CREATE TABLE Service of Service_TY(
  code primary key,
  visit_analysis not null check (visit_analysis in ('v', 'a'))
);
/

CREATE TABLE Doctor of Doctor_TY(
  ID primary key
);
/

CREATE TABLE Assistant of Assistant_TY(
  ID primary key
);
/

CREATE TABLE Habilitation of Habilitation_TY;
/

我想创建一个触发器,当在 Habilitation 中插入一个新元组时,应该检查,如果员工是助理(而不是医生),则 visit_analysis 属性等于 'a' 以了解它是否是合法的元组。

我不知道如何检查员工的类型(如果是医生或助理)。

我会做这样的事情:

create or replace
TRIGGER CHECK_HABILITATION
BEFORE INSERT ON HABILITATION
FOR EACH ROW
DECLARE
BEGIN
    IF (:NEW.EMPLOYEE is of ASSISTANT_TY)
    THEN
      IF :NEW.SERVICE.visit_analysis = 'v'
         THEN
             raise_application_error(-10000, 'invalid tuple');
    END IF;
END;

但它不起作用。我应该如何检查该类型?我得到的错误是: Error(14,4): PLS-00103: Encountered the symbol ";" when expecting one of the following: if

4

2 回答 2

1

尝试将其放入变量中,以下应该可以工作。

create or replace
TRIGGER CHECK_HABILITATION
BEFORE INSERT ON HABILITATION
FOR EACH ROW
DECLARE
  emp employee_TY;
  ser service_TY;
BEGIN
  select deref(:new.employee) into emp from dual;
  if (emp is of (assistant_ty)) then
    select deref(:new.service) into ser from dual;
    if ser.visit_analysis = 'v' then
      raise_application_error('-20001', 'invalid tuple');
    end if;
  end if;
END;
/
于 2021-01-25T18:42:52.433 回答
0

根据IS OF条件的文档,您需要将类型括在括号中,例如:

IF (:NEW.EMPLOYEE is of (ASSISTANT_TY) )

根据https://docs.oracle.com/cd/B28359_01/server.111/b28286/conditions014.htm#SQLRF52157

我不太熟悉使用对象类型,所以可能还有一些我没有看到的其他问题。

于 2021-01-25T16:31:11.937 回答