oracle - 从 SELECT 语句中插入具有 VARRAY 类型数据类型列的表
问题描述
TYPEemp_varray_typ
创建如下:
CREATE TYPE emp_varray_typ AS VARRAY(50) OF VARCHAR2(25);
表 DEPT_ARRAY 创建为 CREATE TABLE dept (DEPT_NO NUMBER, EMP_NM_ARRAY emp_varray_typ);
员工表如下:
**DEPT_NO | EMP_NM**
10 | Scot
10 | Tiger
10 | John
20 | Cindy
20 | Rock
想要将表中的数据作为 2 条记录EMPLOYEE
插入表中(作为数组插入),例如DEPT_ARRAY
EMP_NM
EMP_NM_ARRAY
**DEPT_NO | EMP_NM_ARRAY**
10 | {Scot, Tiger, John}
20 | {Cindy, Rock}
有没有办法使用 SQL 语句插入?
解决方案
您可以使用collect
聚合函数并转换为amp_varray_typ
:
create type emp_varray_typ as varray(50) of varchar2(25);
create table dept (dept_no number, emp_nm_array emp_varray_typ);
create table employee (dept_no, emp_nm) as
select 10, 'Scot' from dual union all
select 10, 'Tiger' from dual union all
select 10, 'John' from dual union all
select 20, 'Cindy' from dual union all
select 20, 'Rock' from dual;
insert into dept (dept_no, emp_nm_array)
select dept_no, cast(collect(emp_nm) as emp_varray_typ)
from employee
group by dept_no;
推荐阅读
- java - 带有POST请求正文的Apache代理HTTPS遵循重定向?
- python - 如何在 PySpark 中比较 2 个数据框中的数据类型和列
- python - 以编程方式添加多个带有绘图图和输入单元的 jupter 笔记本单元
- rust - rust 中 u32 整数类型的使用
- postgresql - 使用网络引导程序 + postgres 设置corda NETWORK
- c# - 如果特定的 Windows 窗体打开,如何停止媒体播放器?视觉工作室 C#
- ruby - 在 Mac 上使用 serverless-ruby-package 的 ruby 应用程序的 Serverless Deploy 崩溃
- python-3.x - 使用 python 为子子项解析 XML
- python-3.x - 读写GML时标签和id的NetworkX问题
- flutter - 如何在 ListView 中本地存储增量计数器?扑