假设我有这些虚拟类型和这些类型的表:
create or replace type Tariff_Plan_TY as object(
tariff_id INTEGER,
call_price DECIMAL(3,2),
sms_price DECIMAL(3,2),
)FINAL;
create or replace type Contract_TY as object(
telephone_no VARCHAR(10),
tariff_plan REF Tariff_Plan_TY
)NOT FINAL;
create or replace
type Operation_TY as object(
id INTEGER,
contract REF Contract_TY
)NOT FINAL;
让我们假设我有不同类型的操作:
create or replace type Call_TY UNDER Operation_TY(
called_no VARCHAR(10),
end_time TIMESTAMP WITH LOCAL TIME ZONE
)FINAL;
create or replace type SMS_TY UNDER Operation_TY(
receiver_no VARCHAR(10)
)FINAL;
我想在一个表上创建一个触发器,Operation_TY
它会告诉我操作成本是多少。要获得这些信息,当在表中插入操作时,我想检查操作的类型,然后进入tariff_plan
合约以查找操作的价格。
我试过:
create or replace
TRIGGER CHECK_OPERATION
BEFORE INSERT ON OPERATION_TB
FOR EACH ROW
DECLARE
contract contract_ty;
tariff_plan tariff_plan_ty;
operation ref operation_ty;
BEGIN
operation = ref :new;
SELECT deref(:new.contract) into contract from dual;
SELECT deref(contract.tariff_plan) into tariff_plan from dual;
if (operation is of (sms_ty)) then
NULL; -- go search the price
elsif (operation is of (call_ty)) then
NULL; -- go search the price
else
NULL;
end if;
END;
这不起作用,我想我错过了一些东西。这个操作应该如何进行?
我在这里留下一个小脚本来随机填充表格:
create table Contract_TB of Contract_TY;
/
create table Tariff_Plan_TB of Tariff_Plan_TY;
/
create table Operation_TB of Operation_TY;
/
create or replace PROCEDURE POPULATE_TARIFF_PLAN AS
idx INTEGER;
BEGIN
idx := 0;
LOOP
INSERT INTO tariff_plan_tb
VALUES (
idx,
DBMS_RANDOM.VALUE,
DBMS_RANDOM.VALUE
);
idx := idx + 1;
EXIT WHEN idx = 10;
end loop;
END POPULATE_TARIFF_PLAN;
/
create or replace
PROCEDURE populate_contract AS
idx INTEGER;
phone_no VARCHAR(10);
ref_tariff_plan REF Tariff_Plan_TY;
BEGIN
idx := 0;
SELECT TO_CHAR(
TRUNC(DBMS_RANDOM.VALUE(1000000000, 9999999999))
) INTO phone_no FROM DUAL;
SELECT * INTO ref_tariff_plan FROM(
SELECT REF(T)
FROM tariff_plan_tb T
ORDER BY DBMS_RANDOM.VALUE
) WHERE rownum <= 1;
INSERT INTO contract_tb
VALUES (phone_no,
ref_tariff_plan);
idx := idx + 1;
EXIT WHEN idx = 500;
END LOOP;
END;
/
create or replace
procedure populate_operation as
idx NUMBER;
ref_contract ref Contract_TY;
begin
idx := 0;
loop
SELECT * into ref_contract FROM(SELECT ref(ct) FROM CONTRACT_TB ct ORDER BY dbms_random.value) WHERE rownum <= 1;
INSERT INTO OPERATION_TB values(
SMS_TY(
idx,
ref_contract
));
idx := idx + 1;
exit when idx = 1000;
end loop;
end;
/