在程序内按 ROWID 选择时出现问题

发布于 2024-08-02 22:12:01 字数 1237 浏览 5 评论 0原文

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

我正在执行一个程序,根据行的 ROWID 从表中检索信息,并使用结果执行一些自定义查询,使用立即执行。我已将问题简化为以下几行,我直接在 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)

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

I've spent hours trying to find a fix to this problem, but since I'm unable to find I've decided to ask here.

I'm doing a procedure to retrieve information from a table based on the row's ROWID and with the results I'm doing some custom query using execute immediate. I've reduced the problem to the following lines, which I'm executing directly in 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;

The previous lines doesn't work as it is. When I print the contents of Consulta it shows:

insert into  values()

but if I eliminate the variable row_id and put it directly in the queries like this, it works:

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;

Displaying what I would expect, something like:

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

This is not work-related, so there is no concerns about performance/security. Thanks for any feedback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我爱人 2024-08-09 22:12:01

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

row_id := CHARTOROWID('AAAEC5AAFAAAADHAAC');

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

You might have to use the CHARTOROWID() conversion function:

row_id := CHARTOROWID('AAAEC5AAFAAAADHAAC');

According to the oracle doc, this would not be necessary for the data type urowid.

有深☉意 2024-08-09 22:12:01

你能得到一个解释计划吗:

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;

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

Can you get an explain plan of this:

select TABLA       from BITACORA where rowid = 'AAAEC5AAFAAAADHAAC';

and this:

select TABLA       from BITACORA where rowid = :1;

It depends on which version of Oracle you are on, but try this:

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;

And then this:

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;

Update your question with the output and that might give some further clues.

浅唱々樱花落 2024-08-09 22:12:01

您为什么选择 DUAL?除非您在简化过程中省略了一些重要的内容,否则这应该对您有用:

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;
/ 

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;
/ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文