在程序内按 ROWID 选择时出现问题
我花了几个小时试图找到解决这个问题的方法,但由于我找不到,所以我决定在这里提问。
我正在执行一个程序,根据行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能必须使用 CHARTOROWID() 转换函数:
根据 oracle doc,这对于数据类型 urowid 来说不是必需的。
You might have to use the CHARTOROWID() conversion function:
According to the oracle doc, this would not be necessary for the data type urowid.
你能得到一个解释计划吗:
这
取决于你使用的Oracle版本,但尝试这个:
然后这个:
用输出更新你的问题,这可能会提供一些进一步的线索。
Can you get an explain plan of this:
and this:
It depends on which version of Oracle you are on, but try this:
And then this:
Update your question with the output and that might give some further clues.
您为什么选择 DUAL?除非您在简化过程中省略了一些重要的内容,否则这应该对您有用:
Why are you selecting from DUAL? Unless you've omitted something vital in your simplification this ought to work for you: