0

我花了几个小时试图找到解决这个问题的方法,但由于我找不到我决定在这里问。

我正在执行一个过程来根据行从表中检索信息,ROWID结果我正在使用execute immediate. 我已将问题简化为以下几行,直接在 SQL Developer 中执行:

declare
  row_id   ROWID;
  consulta VARCHAR2(1000);
begin  
  row_id := 'AAAEC5AAFAAAADHAAC';
  select 'insert into ' || 
    (select TABLA       from BITACORA where rowid = row_id) || ' values(' || 
    (select VALOR_VIEJO from BITACORA where rowid = row_id) || ')' 
    into Consulta from dual;
    DBMS_OUTPUT.PUT_LINE(Consulta);
    --execute immediate Consulta;        
end;

前面的行不能按原样工作。当我打印它的内容Consulta时显示:

insert into  values()

但是如果我消除变量row_id并将其直接放在这样的查询中,它会起作用:

declare
  consulta VARCHAR2(1000);
begin  
  select 'insert into ' || 
    (select TABLA       from BITACORA where rowid = 'AAAEC5AAFAAAADHAAC') || ' values(' || 
    (select VALOR_VIEJO from BITACORA where rowid = 'AAAEC5AAFAAAADHAAC') || ')' 
    into Consulta from dual;
    DBMS_OUTPUT.PUT_LINE(Consulta);
    --execute immediate Consulta;        
end;

显示我所期望的,例如:

insert into MI_TABLA values(1,'Hola','Adios',3,1)

这与工作无关,因此无需担心性能/安全性。感谢您的任何反馈。

4

3 回答 3

1

您可能必须使用 CHARTOROWID() 转换函数:

row_id := CHARTOROWID('AAAEC5AAFAAAADHAAC');

根据oracle doc,这对于数据类型 urowid 来说不是必需的。

于 2009-08-31T10:20:01.227 回答
0

Why are you selecting from DUAL? Unless you've omitted something vital in your simplification this ought to work for you:

declare
  row_id   ROWID;
  consulta VARCHAR2(1000);
begin  
  row_id := 'AAAEC5AAFAAAADHAAC';
  select 'insert into ' ||TABLA || ' values(' ||VALOR_VIEJO|| ')' 
  into   Consulta
  where rowid = row_id; 
  DBMS_OUTPUT.PUT_LINE(Consulta);
  execute immediate Consulta;        
end;
/ 
于 2009-08-31T09:09:34.020 回答
0

你能得到一个解释计划:

select TABLA       from BITACORA where rowid = 'AAAEC5AAFAAAADHAAC';

和这个:

select TABLA       from BITACORA where rowid = :1;

这取决于您使用的 Oracle 版本,但试试这个:

explain plan for
select TABLA       from BITACORA where rowid = 'AAAEC5AAFAAAADHAAC';

select plan_table_output from table(dbms_xplan.display('plan_table',null,'serial'));

delete from plan table;

然后这个:

explain plan for
select TABLA       from BITACORA where rowid = :1;

select plan_table_output from table(dbms_xplan.display('plan_table',null,'serial'));

delete from plan table;

使用输出更新您的问题,这可能会提供一些进一步的线索。

于 2009-08-31T02:56:00.260 回答