在java中执行匿名pl/sql块并获取结果集
我想执行匿名 PL/SQL 并需要获取结果集对象。我得到了可以通过在 PL/SQL 块内使用游标来完成的代码。
但 PL/SQL 块本身将以文本形式来自数据库。所以我无法编辑该 PL/SQL 块。并且它只会返回两个值,其列名始终相同。它将返回 2 列组合值的列表。
这里我给出了示例 PL/SQL。
BEGIN
RETURN 'select distinct fundname d, fundname r from <table> where condition order by 1';
EXCEPTION
WHEN OTHERS THEN
RETURN 'SELECT ''Not Available'' d, ''Not Available'' r FROM dual';
END;
任何回复都会非常有帮助。
I would like to execute the anonymous PL/SQL and need to get the resultset object. I got the code which can be done by using cursors inside the PL/SQL block.
But the PL/SQL block itself will come from the database as text. So I can't edit that PL/SQL block. And it will return only two values whose column names will be same always. It will return list of 2 column combination values.
Here I am giving sample PL/SQL.
BEGIN
RETURN 'select distinct fundname d, fundname r from <table> where condition order by 1';
EXCEPTION
WHEN OTHERS THEN
RETURN 'SELECT ''Not Available'' d, ''Not Available'' r FROM dual';
END;
Any reply will be so helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一个自包含示例,说明如何“执行匿名 PL/SQL 并获取结果集对象”
上面的示例查询“select 1 id, 'hello' name from Dual union select 2, 'peter' from Dual;”可以用任何查询替换。
Here is a self contained example of how to "execute the anonymous PL/SQL and get the resultset object"
The above example query "select 1 id, 'hello' name from dual union select 2, 'peter' from dual;" can be replaced by any query.
首先,您发布的代码无效。匿名 PL/SQL 块无法返回表达式。并且没有 PL/SQL 块可以返回这样的查询结果。您需要执行一些操作,例如声明 REF CURSOR 并使用各种 SQL 语句打开该游标。
由于匿名 PL/SQL 块无法向调用者返回任何内容,因此您描述的体系结构是有问题的。至少,您需要修改匿名块,以便有一个 JDBC 代码可以注册的绑定变量。类似于(改编自 Menon 的专家 Oracle JDBC 编程中的示例 (请注意,我可能引入了一些小的语法错误)
First off, the code you posted is not valid. An anonymous PL/SQL block cannot return an expression. And no PL/SQL block can return the result of a query like that. You would need to do something like declaring a REF CURSOR and opening that cursor using the various SQL statements.
Since an anonymous PL/SQL block cannot return anything to a caller, the architecture you're describing is a problematic. At a minimum, you'd need to modify the anonymous block so that there was a bind variable that your JDBC code could register. Something like (adapted from an example in Menon's Expert Oracle JDBC Programming (note that I may have introduced some minor syntax errors)
尝试这样的操作(伪代码):
返回的 REF CURSOR 可以像普通数据集一样进行处理。
当您使用这样的方法时谨防 SQL 注入...
Try something like this (pseudo-code):
The REF CURSOR which is returned can be processed like a normal dataset.
And beware of SQL injection when you use an approach like this...