我创建了一个自己的类型,构造函数重载:
CREATE TYPE foo_type AS OBJECT(
foo_type INTEGER,
foo_number NUMBER(28, 28),
foo_varchar2 VARCHAR2(4000 CHAR),
CONSTRUCTOR FUNCTION foo_type (data NUMBER) RETURN SELF AS RESULT,
CONSTRUCTOR FUNCTION foo_type (data VARCHAR2) RETURN SELF AS RESULT
)
CREATE TYPE BODY foo_type AS
CONSTRUCTOR FUNCTION foo_type (data NUMBER) RETURN SELF AS RESULT
IS
BEGIN
foo_type := 1;
foo_number := data;
return;
END;
CONSTRUCTOR FUNCTION foo (data VARCHAR2) RETURN SELF AS RESULT
IS
BEGIN
foo_type := 2;
foo_varchar2 := data;
return;
END;
END;
虽然只要它有效,我就不会超载:
insert into test_table (field) values ( foo_type(1, 2.2, 'bar') )
但是当我编写构造函数时,它根本不起作用:
insert into test_table (field) values ( foo_type(2.2) )
或
insert into test_table (field) values ( foo_type('bar') )
此时oracle说:“ORA-06553:声明太多'foo_type'匹配此调用” 因此:
insert into test_table (field) values ( foo_type(foo_varchar2 => 'bar') )
说:“ORA-009007:缺少右括号”。
我应该改变什么才有效?
感谢。
答案 0 :(得分:4)
如果我更正了语法错误,那么声明和对象的主体编译
foo_type
而不是foo
- 构造函数始终使用对象的名称。给我留下像
这样的东西CREATE TYPE foo_type AS OBJECT(
foo_int INTEGER,
foo_number NUMBER(28, 28),
foo_varchar2 VARCHAR2(4000 CHAR),
CONSTRUCTOR FUNCTION foo_type (data NUMBER) RETURN SELF AS RESULT,
CONSTRUCTOR FUNCTION foo_type (data VARCHAR2) RETURN SELF AS RESULT
);
/
CREATE OR REPLACE TYPE BODY foo_type AS
CONSTRUCTOR FUNCTION foo_type (data NUMBER) RETURN SELF AS RESULT
IS
BEGIN
foo_int := 1;
foo_number := data;
return;
END;
CONSTRUCTOR FUNCTION foo_type (data VARCHAR2) RETURN SELF AS RESULT
IS
BEGIN
foo_int := 2;
foo_varchar2 := data;
return;
END;
END;
如果我更改为传递对NUMBER(28,28)
列有效的数字(2.2不是),则重载工作正常
SQL> ed
Wrote file afiedt.buf
1 declare
2 l_foo foo_type;
3 begin
4 l_foo := foo_type( 0.2 );
5 dbms_output.put_line( l_foo.foo_int );
6 l_foo := foo_type( 'bar' );
7 dbms_output.put_line( l_foo.foo_int );
8* end;
SQL> /
1
2
PL/SQL procedure successfully completed.
或插入表格
SQL> create table test_table( foo_column foo_type );
Table created.
SQL> insert into test_table values( foo_type( 0.2 ) );
1 row created.
SQL> insert into test_table values( foo_type( 'bar' ) );
1 row created.
我觉得你真的不希望foo_number
成为NUMBER(28, 28)
,尤其是当您尝试在示例中指定值2.2时。如果您的实际对象类型是这样声明的,那么您的默认构造函数就会失败。