将单行结果集转换为关联数组
假设我有以下查询:
select 1 foo, 2 bar, 3 baz from dual;
基本上,该查询总是返回一行。我需要从中创建一个关联数组,其中之一:
arr('foo') = 1;
arr('bar') = 2;
arr('baz') = 3;
我知道结果集只有一行。我既不知道列数量也不知道列名称。
有什么想法吗?
谢谢。
更新:
我的一个朋友发现了一个漂亮而优雅的涉及 XML 的解决方案:
SELECT
XMLTYPE(EXTRACT(VALUE(T), '/*') .GETSTRINGVAL()) .GETROOTELEMENT() NODE,
EXTRACTVALUE(COLUMN_VALUE, '/*') NODEVALUE
FROM
TABLE(XMLSEQUENCE(XMLTYPE((CURSOR
(
--this is the query that needs to be transformed
SELECT
*
FROM
some_table
WHERE some_table.id = 123
)
)) .EXTRACT('/ROWSET/ROW/*'))) T;
Say, I have the following query:
select 1 foo, 2 bar, 3 baz from dual;
Basically, this query always returns me one row. I need to create an associative array from that, one of this kind:
arr('foo') = 1;
arr('bar') = 2;
arr('baz') = 3;
I know that result set is only one row. I don't know neither columns amount nor columns names.
Any ideas?
Thanks.
upd:
A friend of mine found a nice and elegant, XML-involving solution:
SELECT
XMLTYPE(EXTRACT(VALUE(T), '/*') .GETSTRINGVAL()) .GETROOTELEMENT() NODE,
EXTRACTVALUE(COLUMN_VALUE, '/*') NODEVALUE
FROM
TABLE(XMLSEQUENCE(XMLTYPE((CURSOR
(
--this is the query that needs to be transformed
SELECT
*
FROM
some_table
WHERE some_table.id = 123
)
)) .EXTRACT('/ROWSET/ROW/*'))) T;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个简单的包,它从键值对表中填充关联数组。这是最简单的情况。
而且它工作得非常巧妙......
但是您想要的更复杂:在不知道查询投影的情况下动态填充数组。以下是一个极其简单的实现,如果相关表包含单行,则该实现将起作用。它可以轻松扩展以处理具有多行的表。
因此,我们重载了 POP() 过程:
因此,这也有效:
我不太担心以如此糟糕的方式初始化数组对性能的影响。鉴于记录保留在缓存中,这还不算太糟糕。如果您频繁调用
POP()
以至于性能确实成为一个问题,那么您可能不应该首先使用关联数组:常规查找或结果集缓存都将是更好的选择。Here is a simple package which populates an associative array from a key value pair table. This is the easy case.
And it works quite neatly....
But what you want is more complicated: to dynamically populate the array without knowing the projection of the query. The following is a horribly simplistic implementation, which will work if the table in question contains a single row. It can easily be extended to handle a table with multiple rows.
We overload the
POP()
procedure thus:So, this works too:
I'm not too worried about the performance hit of initialising the array in such a shonky fashion. Given that the record stays in cache it isn't too bad. And if you're calling
POP()
so often that performance does become an issue then you probably shouldn't be using an associative array in the first place: either regular look-ups or resultset caching would be better options.一个非常简短的答案是使用
DBMS_SQL
包查看旧式(Oracle 9 之前)动态 SQL。您可以使用DESCRIBE_COLUMNS
获取列数(和数据类型),并获取DESCRIBE_COLUMNS
返回的 PL/SQL 表中每个条目的COLUMN_VALUE
code> 单独获取每个列值。之后,将一行转换为关联数组就非常容易了。
A very short answer is to look at old-style (pre Oracle 9) dynamic SQL using the
DBMS_SQL
package. You can use theDESCRIBE_COLUMNS
to get the number (and data types) of the columns andCOLUMN_VALUE
for every entry in the PL/SQL table returned byDESCRIBE_COLUMNS
to fetch each column value individually.After that, it's pretty easy to turn a row into an associative array.