在Oracle 11g中调用成员过程

时间:2010-02-24 13:08:25

标签: sql oracle ora-01722

让我们说:

create type address as object (
   line1 varchar2(50),
   city varchar2(50),
   member procedure insert_address(line1 varchar2, city varchar2)
)
/

create table address_table of address;

create type body address as
   member procedure insert_address(line1 varchar2, city varchar2) is
   begin
       insert into address_table values (line1, city);
       commit;
   end insert_address;
end;
/

如何致电insert_address

通过执行以下操作,我得到invalid number or types of arguments

begin
   address.insert_address('123 my road','london');
end;

我能做到这一点并且有效,但似乎是一个坏主意:

declare
  v_address address := new address(null,null);
begin
  v_address.insert_address('123 my road','london');
end;

由于

2 个答案:

答案 0 :(得分:2)

使用static代替member执行您的程序:

static procedure insert_address(line1 varchar2, city varchar2)

然后你可以在对象类型而不是实例上调用它:

address.insert_address('123 my road','london');

有关详细信息,请参阅Using PL/SQL Object Types

答案 1 :(得分:0)

当你构建它时(奇怪),过程insert_address只能在address类型的对象的上下文中调用,并且必须使用参数line1和city调用,其值与该对象没有任何关联你称之为“为”。这就是我如何构建表和代码,并使用它:

create table address_table (line1 varchar2(50), city varchar2(50));

create package address_pkg as
   procedure insert_address(p_line1 varchar2, p_city varchar2);
end;
/

create package body address_pkg as
   procedure insert_address(p_line1 varchar2, p_city varchar2) is
   begin
      insert into address_table (line1, city) values (p_line1, p_city);
   end;
end;
/

exec address_pkg.insert_address ('123 my road', 'london');

使用你的funkier模型,似乎insert_address过程应该将“地址对象本身”插入到表中。类似的东西:

create type address as object (
   line1 varchar2(50),
   city varchar2(50),
   member procedure insert_address
)
/

create table address_table of address;

create type body address as
   member procedure insert_address is
   begin
       insert into address_table values (line1, city);
       commit;
   end insert_address;
end;
/

然后插入就像:

declare
  v_address address := new address('123 my road','london');
begin
  v_address.insert_address;
end;