sql - Oracle批量收集成三列
问题描述
如何在下面的函数中添加多个例如。l_col2
和l_col3
l_col2 = Select sal as val2 from emp where empno = P_ID
l_col3 = Select deptno as val3 from emp where empno = P_ID
create or replace function F_MY_FUNC
(
P_ID IN number
)
return sys.odcinumberlist
as
l_coll sys.odcinumberlist;
begin
select * bulk collect into l_coll
from (
Select ename as val from emp where empno = P_ID
union all
Select ename as val from myemp where empno = P_ID
);
return l_coll;
end;
解决方案
你可以,但不能进入SYS.ODCINUMBERLIST
- 创建你自己的类型。
SQL> create or replace type t_row as object
2 (empno number, ename varchar2(10), job varchar2(10));
3 /
Type created.
SQL> create or replace type t_tab as
2 table of t_row;
3 /
Type created.
SQL> declare
2 l_tab t_tab;
3 begin
4 select t_row(empno, ename, job) --> 3 columns
5 bulk collect into l_tab --> into one collection
6 from emp
7 where deptno = 20;
8 end;
9 /
PL/SQL procedure successfully completed.
SQL>
推荐阅读
- php - 根据父级重新排列数组
- cuda-gdb - 在 cuda-gdb 打印命令中取消引用 float2 / struct 没有给出正确的值
- php - 在 Laravel 之外使用 Laravel 8 应用功能
- android-studio - 片段更改加载时间缓慢 - Android Studio
- redis - 是否可以管道 Redis 布隆过滤器命令?
- snowflake-cloud-data-platform - 在上次数据加载查询中出现验证错误时出错
- next.js - 如果缺少 prop-type,Nextjs 组件不会显示警告或错误
- javascript - 有没有更好的写法|| 在单个 if 语句中
- dolphindb - 使用 dolphindb python api 时出现运行时错误
- javascript - 我无法导出通过 express 模块提供的承诺中的 URL 值