暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 将结果集存储到表列中

askTom 2015-11-24
141

问题描述

嗨,汤姆,

我想将子查询的结果存储到表的单个记录中...

我找到了用于“折叠”子查询记录的游标关键字:
select sql_text, sql_fulltext, sql_id, hash_value, child_address , 
        cursor(select name, position, datatype, datatype_string, max_length, last_captured, value_string  
                from v$sql_bind_capture b where a.hash_value=b.hash_value and a.child_address=b.child_address
              ) bind_values
from v$sql a 


在Toad中,列“bind_values”显示为“(curSOR)”。如果是双单击,则显示7个详细信息列

但如何将此查询的结果存储到表中呢?
“创建表为...”无法工作,因为“未授权游标”...
是否可以将"CursOR"的结果“转换”为表兼容的数据类型?
我是否应该更好地使用其他方法(不使用"CurSOR" ) ?
当然,我希望能够解码存储字段以在以后提取值(例如,使用“从xxx中选择a, b, table(... )”指令)

我知道v$sql已经将绑定值存储到类型为“raw(2000)”的“BIND_DATA”列
我可以用
select a.sql_id, a.sql_fulltext,  b.position, b.datatype_string, b.value_string, b.name
from v$sql a, table(dbms_sqltune.extract_binds(a.bind_data)) b

...但很遗憾,绑定值的“名称”未填充...是虫子吗?

谢谢你的帮助
致以问候,
克里斯多夫

专家解答

游标()返回结果集。它是PL/SQL中等效的ref游标。你不能把这些放在表里-这说不通。

您可以用许多其他方法将子查询存储在单个列中,例如XML、JSON或嵌套表。

下面是如何使用嵌套表执行此操作:

create table par (
  id integer not null primary key
);

create table chld (
  par_id integer not null
    references par (id),
  seq    integer not null,
  primary key (par_id, seq)
);

insert into par 
  select rownum from dual connect by level <= 3;
  
insert into chld
  with rws as (
    select rownum r from dual connect by level <= 3
  )
    select id, r from par, rws
    where  id <= r;
    
create or replace type tp as object (
  par_id integer,
  seq    integer
);
/
create or replace type tp_arr as table of tp;
/


然后,可以创建一个具有嵌套表类型的表,并使用强制转换(收集)填充该表。一旦这些类型就位,就可以使用table()运算符提取这些值:

create table temp (
  id, children
) nested table children store as chl_tab 
as 
  select id, 
         (select cast (collect (tp(par_id, seq)) as tp_arr) from chld where par_id = id) children
  from   par;
  
select c.*
from   temp, table(temp.children) c;

    PAR_ID        SEQ
---------- ----------
         1          1
         1          2
         1          3
         2          2
         2          3
         3          3

也就是说,除非您有特殊的理由使用嵌套表,否则我更愿意将结果存储在两个单独的表中。一个用于没有子查询的查询,另一个用于存储子查询的结果。

请注意, dbms_sqltune.extext_binds没有文档化,因此使用时应小心。也就是说,如果您检查封装规格,您可以看到:

  -- NOTE:
  --     name of the bind in SQL_BIND object is not populated by this function


有关收集的详细信息,请参阅:

http://www.oracle-developer.net/display.php?id=514
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论