我有以下表格类型:
CREATE OR REPLACE TYPE tab_id AS TABLE OF NUMBER(12);
我需要从表中选择ID并将它们作为单个tab_id返回。在PL / SQL中我会使用BULK COLLECT INTO,但我需要在纯SQL 中使用它。我需要查询直接返回tab_id对象。
我试过了:
WITH src AS ( SELECT 1 AS token FROM DUAL UNION ALL SELECT 2 AS token FROM DUAL ) SELECT tab_id( token ) FROM src
但是这会返回两行tab_id
,第一行包含值1,第二行包含值2。
我需要查询只返回一行,其中包含两个值tab_id
。
我也试过以下版本但没有成功:
WITH src AS ( SELECT 1 FROM DUAL UNION ALL SELECT 2 FROM DUAL ) SELECT tab_id( SELECT * FROM src ) FROM DUAL -- I got ORA-00936 SELECT tab_id( ( SELECT * FROM src ) ) FROM DUAL -- I got ORA-01427
有没有办法如何将所有值选择为一种表类型?
答案 0 :(得分:1)
您可以将COLLECT
功能与CAST
一起使用。
Oracle 11g R2架构设置:
CREATE OR REPLACE TYPE tab_id AS TABLE OF NUMBER(12);
查询1 :
WITH src
AS (
SELECT 1 AS token FROM DUAL
UNION ALL
SELECT 2 AS token FROM DUAL
)
SELECT CAST(COLLECT(token) AS tab_id)
FROM src
<强> Results 强>:
| CAST(COLLECT(TOKEN)ASTAB_ID) |
|------------------------------|
| 1,2 |