【问题标题】:Select into multilevel type in Oracle在Oracle中选择多级类型
【发布时间】:2020-07-02 13:50:33
【问题描述】:

我正在尝试找出是否有办法在 Oracle 中批量收集到多级类型。下面的示例应该有助于解释我正在尝试做的概念。

有一个带有非规范化县镇列表的源表:

create table county_town (county varchar2(20), town varchar2(20));

insert into county_town values ('Surrey', 'Dorking');
insert into county_town values ('Surrey', 'Woking');
insert into county_town values ('Surrey', 'Guildford');
insert into county_town values ('Oxfordshire', 'Thame');
insert into county_town values ('Oxfordshire', 'Abingdon');

我想要做的是将它加载到一个看起来像这样的多级类型中:

create type towns_typ as table of varchar2(20);
create type counties_typ as object (country varchar2(20), towns towns_type);    
create type nt_counties_typ as table of counties_typ;

l_county_data nt_counties_typ 

有没有什么方法可以写一个 SELECT 语句来批量收集这些数据到 l_county_data 表中?如果无法使用 BULK COLLECT 是否有另一种方法可以简单地做到这一点?

【问题讨论】:

    标签: oracle plsql


    【解决方案1】:

    是的,就像这里:

    declare 
      l_county_data nt_counties_typ;
    begin
      select counties_typ(county, cast(collect(town) as towns_typ))
      bulk collect into l_county_data
      from county_town
      group by county;
    
      dbms_output.put_line(l_county_data(2).county);
    end;
    

    dbfiddle

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多