对象使用动态SQL

时间:2015-09-24 19:37:30

标签: oracle object plsql

我有一个对象,我想动态更新一个对象表。作为一个例子

create or replace type typA as object
(
  a number,
  member procedure insert_self
);
/
create or replace type body typA is
  member procedure insert_self is
  begin
    null;
  end;
end;
/
create table typA_table of typA;
/
create or replace type body typA is
  member procedure insert_self is
  sql_stmt varchar2(200);
  begin
    sql_stmt := 'insert into typ_a_table values(self)';
  execute immediate sql_stmt;
  end;
end;
/
declare
  l_typ typA;
begin
  l_typ := typA(123);
  l_typ.insert_self;
end;

但由于指定了“自我”的行,因此会返回错误。在sql_stmt中。

Error report -
ORA-04063: table "SYSTEM.TYP_A_TABLE" has errors
ORA-06512: at "SYSTEM.TYPA", line 6
ORA-06512: at line 5
04063. 00000 -  "%s has errors"
*Cause:    Attempt to execute a stored procedure or use a view that has
           errors.  For stored procedures, the problem could be syntax errors
           or references to other, non-existent procedures.  For views,
           the problem could be a reference in the view's defining query to
           a non-existent table.
           Can also be a table which has references to non-existent or
           inaccessible types.
*Action:   Fix the errors and/or create referenced objects as necessary.

在我引用不易转换为字符串的变量时,是否存在解决此错误的问题?

1 个答案:

答案 0 :(得分:0)

你能不能简单地将你的会员程序改为如下所示,似乎对我有用。

create or replace type body typA is
  member procedure insert_self is
  sql_stmt varchar2(200);
  begin
    insert into typ_a_table values(self.a);
  end;
end;