有一个MODEL表,如下所示
create table MODEL (
model_name VARCHAR(20),
model_year NUMBER,
consumption VARCHAR(6) NOT NULL,
Component component_ty_nt,
Distributor distributor_ty_nt,
Car car_ty_nt,
constraint t_pk primary key (Model_Name, Model_Year))
nested table Component store as component_ty_nt_TAB
nested table Distributor store as distributor_ty_nt_TAB
nested table Car store as car_ty_nt_TAB
Component_ty
Create or replace type Component_ty as object (
Code VARCHAR(10),
Component_Description VARCHAR(100),
Component_Type VARCHAR(10))
NOT FINAL;
具有component_type属性,该属性可以是'body'、'engine'或'equipment'
每次我在这个表上执行INSERT时,我都需要一个触发器来检查类型的出现情况,如果Boyd!= 1或'Engine'!= 1,则返回错误。
CREATE OR REPLACE TRIGGER check_engine_and_body
BEFORE INSERT ON model
FOR EACH ROW
DECLARE
engineCounter NUMBER;
bodyCounter NUMBER;
BEGIN
FOR i IN 1..:new.Component.COUNT LOOP
IF deref(:new.Component(i)).Component_Type = 'Engine' THEN
engineCounter := engineCounter + 1;
ELSIF deref(:new.Component(i)).Component_Type = 'Body' THEN
bodyCounter := bodyCounter + 1;
END IF;
END LOOP;
IF engineCounter != 1 OR bodyCounter != 1 THEN
RAISE_APPLICATION_ERROR(-20000, 'Incorrect occurrences for component_type attribute');
END IF;
END;
但我得到错误:
PL/SQL: Statement ignore
PLS-00306: wrong number or types of arguments in call to 'DEREF'
如何修复?
1条答案
按热度按时间wooyq4lh1#
我还没有弄清楚为什么你的触发器会失败,但是使用SQL检查(而不是PL/SQL)。
我假设你有类型和表:
你可以使用trigger:
对于样本数据:
然后:
作品及:
每个失败,并返回错误:
fiddle