【问题标题】:How to update/insert values in nested table of values while preserving existing values如何在保留现有值的同时更新/插入嵌套值表中的值
【发布时间】:2018-11-04 23:55:39
【问题描述】:

在我们的 Oracle 11g 数据库中,我有一个自定义数据类型:

num_list

create or replace type
    num_list
as
    table of varchar2(25);

然后我创建了一个使用此数据类型的表:

create table num_list_table(
    id number(*,0)
    ,numbers num_list
) nested table numbers store as numbers_tab
;

然后我插入了以下行:

insert into num_list_table values (1, num_list('123', 456'));

我现在有 1 行。我试图弄清楚如何在第 1 行的 num_list 中插入更多值,同时保留任何现有值,而不必手动输入这些现有值;这是一个更大的任务的一个小例子,需要在保留 val 的同时进行大量更新。

我很接近这个:

update
    num_list_table
set numbers = (select num_list(numbers) from (
    select listagg(numbers, ',') within group (order by numbers) numbers
    from (select t.column_value numbers
          from 
              num_list_table nlt,
              table(nlt.numbers) t
           where
               st.id = 1
         union
         select '789'
         from dual)))
     where id = 1;

但是,这些结果是一个条目:

num_list('123,456,789')

我需要它们是 num_list 中的多个条目:

num_list('123', '456', '789')

我们将不胜感激。

【问题讨论】:

标签: sql oracle oracle11g custom-type


【解决方案1】:

您可以将单个值插入到特定 ID 的嵌套表中,如下所示:

insert into table 
       (select numbers 
          from num_list_table 
          where id = 1)
values ('123')

或使用插入选择:

insert into table 
       (select numbers 
          from num_list_table 
         where id = 1)
select n.same_type_field
  from another_table n;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多