You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle APEX插入客户至多态人员表遇ORA-00907错误求助

问题描述

我正在使用Oracle数据库(Oracle APEX)构建一个包含员工、客户、书籍、副本及租赁的对象关系型数据库。人员表pessoa_tb_or支持多态,可存储员工和客户数据。但插入客户数据时触发ORA-00907: missing right parenthesis错误,仅最后一条INSERT语句报错,即使单独创建客户表也会出现相同问题。

错误的INSERT语句:

insert into pessoa_tb_or values(
cliente_typ(7, 'CLIENTE JUNIOR', 1, to_date('01/01/2020'), telefones_typ('9999-9999', '9999-8888'), 
locacoes_nt_typ((1, to_date('01/01/2022'), (select ref(e) from exemplar_tb_or e where cod_exemplar = 1) ), (2, to_date('01/05/2022'), (select ref(e) from exemplar_tb_or e where cod_exemplar = 2) ) )))

完整数据库创建及插入代码:

create type livro_typ as object(
    cod_livro int,
    titulo varchar2(50),
    autor varchar2(50)
);

create type exemplar_typ as object(
    cod_exemplar int,
    data_compra date,
    livro ref livro_typ
);

create type pessoa_typ as object(
    cpf int,
    nome varchar2(50)
)not final;

create type funcionario_typ under pessoa_typ(
    cod_funcionario int,
    turno varchar2(20)
);

create type telefones_typ as varray(3) of varchar2(20);

create type locacao_typ as object(
    cod_locacao int,
    data_aluguel date,
    exemplar_livro ref exemplar_typ
);

create type locacoes_nt_typ as table of locacao_typ;

create type cliente_typ under pessoa_typ(
    cod_cliente int,
    data_cadastro date,
    telefones telefones_typ,
    alugueis locacoes_nt_typ
);

create table pessoa_tb_or of pessoa_typ(
    primary key(cpf),
    nome not null
);

create table livro_tb_or of livro_typ(
   primary key(cod_livro),
   titulo not null,
   autor not null
);

create table exemplar_tb_or of exemplar_typ(
   primary key(cod_exemplar),
   data_compra not null,
   livro not null
);

insert into pessoa_tb_or values(pessoa_typ(1, 'PESSOA JOAO'));
insert into pessoa_tb_or values(funcionario_typ(2, 'FUNCIONARIO PEDRO', 1, 'MANHA'));
insert into pessoa_tb_or values(funcionario_typ(3, 'FUNCIONARIO HENRIQUE', 2, 'MANHA'));
insert into pessoa_tb_or values(funcionario_typ(4, 'FUNCIONARIO LAURA', 3, 'TARDE'));
insert into pessoa_tb_or values(funcionario_typ(5, 'FUNCIONARIO LUIZA', 4, 'TARDE'));
insert into pessoa_tb_or values(funcionario_typ(6, 'FUNCIONARIO LETICIA', 5, 'NOITE'));

insert into livro_tb_or values(livro_typ(1, 'COMPUTACAO PARA LEIGOS', 'HENRIQUE FEITOSA'));
insert into livro_tb_or values(livro_typ(2, 'BANCO DE DADOS PARA LEIGOS', 'HENRIQUE FEITOSA'));
insert into livro_tb_or values(livro_typ(3, 'APRENDIZAGEM DE MAQUINA PARA LEIGOS', 'MARIA BRAGA'));
insert into livro_tb_or values(livro_typ(4, 'MATEMATICA PARA LEIGOS', 'MARIA BRAGA'));
insert into livro_tb_or values(livro_typ(5, 'FISICA PARA LEIGOS', 'FERNANDO LIMA'));

insert into exemplar_tb_or values(exemplar_typ(1, to_date('01/01/2020'), (select ref(l) from livro_tb_or l where cod_livro = 1) ));
insert into exemplar_tb_or values(exemplar_typ(2, to_date('01/01/2021'), (select ref(l) from livro_tb_or l where cod_livro = 2) ));
insert into exemplar_tb_or values(exemplar_typ(3, to_date('01/01/2022'), (select ref(l) from livro_tb_or l where cod_livro = 3) ));
insert into exemplar_tb_or values(exemplar_typ(4, to_date('01/01/2019'), (select ref(l) from livro_tb_or l where cod_livro = 4) ));
insert into exemplar_tb_or values(exemplar_typ(5, to_date('01/01/2019'), (select ref(l) from livro_tb_or l where cod_livro = 5) ));

insert into pessoa_tb_or values(
    cliente_typ(7, 'CLIENTE JUNIOR', 1, to_date('01/01/2020'), telefones_typ('9999-9999', '9999-8888'), 
    locacoes_nt_typ((1, to_date('01/01/2022'), (select ref(e) from exemplar_tb_or e where cod_exemplar = 1) ), (2, to_date('01/05/2022'), (select ref(e) from exemplar_tb_or e where cod_exemplar = 2) ) )
));
问题排查与修复

错误原因是构造locacoes_nt_typ集合时,直接用括号包裹属性值而未显式调用locacao_typ构造函数。Oracle对象类型必须通过构造函数实例化,不能仅用括号传递参数。

修正后的INSERT语句:

insert into pessoa_tb_or values(
    cliente_typ(7, 'CLIENTE JUNIOR', 1, to_date('01/01/2020'), telefones_typ('9999-9999', '9999-8888'), 
    locacoes_nt_typ(
        locacao_typ(1, to_date('01/01/2022'), (select ref(e) from exemplar_tb_or e where cod_exemplar = 1)), 
        locacao_typ(2, to_date('01/05/2022'), (select ref(e) from exemplar_tb_or e where cod_exemplar = 2))
    )
));

额外补充:所有CREATE TYPE和CREATE TABLE语句末尾建议添加分号,避免语法解析问题(原代码中部分语句缺少分号)。

内容的提问来源于stack exchange,提问作者Vinicius Alves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:15:40