从 Oracle SQL 中的函数查询 table 数据类型

Query table datatype from function in Oracle SQL

同事

我对 Oracle 中的 table 函数有疑问。

更具体地说,我有一个将 BLOB 转换为 varchar2 的 table 的函数。

create type string_array is table of varchar2(255);

create or replace function blob_to_strings(p_blb in BLOB) return string_array as
begin
-- some processing here
end;

我还有 table 包含我需要使用的 BLOBS。

create table my_blobs (id number, data blob)

现在,在 my_blobs table 中有 id,我想查询转换函数的结果。像

select t.* from table(blob_to_strings(b.data)) t, my_blobs b where b.id = 123;

(我知道这是不正确的,只是展示我所需要的) 此查询预计 returns b.data: invalid identifier,因为您无法访问 from 部分内的其他 table 列。

我在 PL/SQL 运行 2 个查询中了解如何做,但确实需要在 SQL.

中做

有人可以帮助我吗? 提前谢谢你。

UPD: 我试过以下方法:

select * from table(select blob_to_strings(b.data) from my_blobs b where b.id = 123);

结果: ORA-00902: 无效数据类型

还有其他想法吗?

您可以使用 Oracle 的 PIPE ROW 语句实现此目的

参见:Pipelined Table Functions

您的原始查询的问题可能是您在尝试从数组 (from table(blob_to_strings(b.data)) t, my_blobs b) 中获取 select 之后出现了 table 名称。换句话说,您试图从尚未声明的内容中 select。切换 from 子句中项目的顺序,它应该可以工作。

这是我用来演示的测试用例(我使用了 CLOB,因为我们显然是在处理文本;我不确定您为什么要使用 BLOB?):

create table t1 (id number,
                 clob_col clob);

insert into t1 values (1, 'abcd');
insert into t1 values (2, 'efg');

commit;

create type string_array is table of varchar2(255);

create or replace function clob_to_str_tab (p_clob in clob)
return string_array
is
  v_str_arr string_array := string_array();
begin
  for i in 1.. length(p_clob)
  loop
    v_str_arr.extend;
    v_str_arr(i) := substr(p_clob, i, 1);
  end loop;

  return v_str_arr;
end;
/

select t1.id,
       t2.column_value res
from   table(clob_to_str_tab(t1.clob_col)) t2,
       t1;

ORA-00904: "T1"."CLOB_COL": invalid identifier

select t1.id,
       t2.column_value res
from   t1,
       table(clob_to_str_tab(t1.clob_col)) t2;

        ID RES
---------- ---
         1 a  
         1 b  
         1 c  
         1 d  
         2 e  
         2 f  
         2 g