在 PL/SQL 函数中的 EXECUTE IMMEDIATE 中使用 UDT 变量



我正在使用Oracle 11g在PL/SQL上构建一个函数。

我正在尝试在 EXECUTE IMMEDIATE 语句中使用表变量,但它不起作用,如您所见:

ERROR at line 1:
ORA-00904: "CENTER_OBJECTS": invalid identifier
ORA-06512: at "HIGIIA.KNN_JOIN", line 18

我使用的代码是...

一、类型定义

CREATE TYPE join_t IS OBJECT (
inn                          char(40),
out                        char(40)
);
/

CREATE TYPE join_jt IS TABLE OF join_t;
/
CREATE TYPE blob_t IS OBJECT (
id           CHAR(40),
fv           BLOB
);
/
CREATE TYPE blob_tt IS TABLE OF blob_t;
/

函数为:

create or replace FUNCTION knn_join (tab_inn IN varchar2, tab_out IN varchar2, blob_col1 IN varchar2, blob_col2 IN varchar2, dist_alg in VARCHAR2, kv in NUMBER ) RETURN join_jt
IS
var_fv BLOB;
var_id CHAR(40);
center_objects blob_tt := blob_tt();
retval join_jt := join_jt ();
join_table join_jt := join_jt();
sql_stmt1 varchar2(400);
sql_stmt2 varchar2(400);
BEGIN
sql_stmt1 := 'SELECT blob_t(ROWIDTOCHAR(rowid),' || blob_col1 || ') FROM ' || tab_out;
sql_stmt2 := 'SELECT join_t(ROWIDTOCHAR(r.rowid), center_objects(idx).id) FROM ' || tab_inn || ' r  WHERE ' || dist_alg || '_knn(r.' || blob_col2 || ',  center_objects(idx).' ||   blob_col1 || ')<=' || kv;
dbms_output.put_line(sql_stmt2);    
EXECUTE IMMEDIATE sql_stmt1 BULK COLLECT INTO center_objects;
for idx in center_objects.first()..center_objects.last()
loop
--SELECT join_t(ROWIDTOCHAR(r.rowid), center_objects(idx).id) BULK COLLECT INTO join_table FROM londonfv r WHERE manhattan_knn(r.fv, center_objects(idx).fv) <=5;
EXECUTE IMMEDIATE sql_stmt2 BULK COLLECT INTO join_table;   
for idx2 in join_table.first()..join_table.last()
loop
retval.extend();
retval(retval.count()) := join_table(idx2);
end loop;
end loop;
RETURN retval;
END;
/

要运行函数:

select * from TABLE(knn_join('london','cophirfv','fv','fv','manhattan',5)); 

我正在尝试使用立即执行运行语句'SELECT join_t(ROWIDTOCHAR(r.rowid), center_objects(idx).id) 批量收集到join_table从伦敦 R manhattan_knn(r.fv, center_objects(idx).fv) <=5' 中,但它不起作用,因为我在其中使用了变量。

有人可以帮我一把吗?

提前感谢!

您不能在动态 SQL 语句中引用本地 PL/SQL 变量,因为它超出了动态调用使用的 SQL 上下文的范围。您可以替换您的第一个呼叫:

SELECT join_t(ROWIDTOCHAR(r.rowid), center_objects(idx).id) FROM ' ...

使用绑定变量:

SELECT join_t(ROWIDTOCHAR(r.rowid), :id FROM ' ...
EXECUTE IMMEDIATE ... USING center_objects(idx).id ...

但是当对象属性也是可变的时,您也无法执行以下操作:

... ',  center_objects(idx).' ||   blob_col1 || ')<='...

虽然 - 至少在您展示的示例中 - 唯一可用的对象属性名称是fv,无论传递给函数的表列名称如何 - 因此可以硬编码; 因此可以使用绑定变量:

... ',  :fv)<='...
EXECUTE IMMEDIATE ... USING center_objects(idx).id, center_objects(idx).fv ...

并且kv值也应该是一个绑定变量,因此您最终会得到:

create or replace FUNCTION knn_join (tab_inn IN varchar2, tab_out IN varchar2,
blob_col1 IN varchar2, blob_col2 IN varchar2, dist_alg in VARCHAR2, kv in NUMBER )
RETURN join_jt
IS
center_objects blob_tt := blob_tt();
retval join_jt := join_jt ();
join_table join_jt := join_jt();
sql_stmt1 varchar2(400);
sql_stmt2 varchar2(400);
BEGIN
sql_stmt1 := 'SELECT blob_t(ROWIDTOCHAR(rowid),' || blob_col1 || ') FROM ' || tab_out;
sql_stmt2 := 'SELECT join_t(ROWIDTOCHAR(r.rowid), :id) FROM ' || tab_inn || ' r  WHERE '
|| dist_alg || '_knn(r.' || blob_col2 || ',  :fv)<= :kv';
dbms_output.put_line(sql_stmt1);    
dbms_output.put_line(sql_stmt2);    
EXECUTE IMMEDIATE sql_stmt1 BULK COLLECT INTO center_objects;
for idx in center_objects.first()..center_objects.last()
loop
EXECUTE IMMEDIATE sql_stmt2 BULK COLLECT INTO join_table
USING center_objects(idx).id, center_objects(idx).fv, kv;   
for idx2 in join_table.first()..join_table.last()
loop
retval.extend();
retval(retval.count()) := join_table(idx2);
end loop;
end loop;
RETURN retval;
END;
/

据我所知,您仍然可以在动态SQL语句中进行连接,并消除循环以及对中间center_objectsjoin_table集合的需求:

create or replace FUNCTION knn_join (tab_inn IN varchar2, tab_out IN varchar2,
blob_col1 IN varchar2, blob_col2 IN varchar2, dist_alg in VARCHAR2, kv in NUMBER )
RETURN join_jt
IS
retval join_jt;
sql_stmt varchar2(400);
BEGIN
sql_stmt := 'SELECT join_t(ROWIDTOCHAR(tinn.rowid), ROWIDTOCHAR(tout.rowid))'
|| ' FROM ' || tab_inn || ' tinn JOIN ' || tab_out || ' tout'
|| ' ON ' || dist_alg || '_knn(tinn.fv, tout.fv) <= :kv';
dbms_output.put_line(sql_stmt);
EXECUTE IMMEDIATE sql_stmt BULK COLLECT INTO retval USING kv;
RETURN retval;
END;
/

当您按照所示调用它时:

select * from TABLE(knn_join('london','cophirfv','fv','fv','manhattan',5)); 

这相当于硬编码:

SELECT join_t(ROWIDTOCHAR(tinn.rowid), ROWIDTOCHAR(tout.rowid))
FROM london tinn
JOIN cophirfv tout
ON manhattan_knn(tinn.fv, tout.fv) <= 5

。所以我想你可以验证那个硬编码版本是否首先给你带来了你期望的结果。(当然,在问题中添加样本数据和预期结果会有所帮助)。

该连接条件可能很昂贵,具体取决于函数正在执行的操作、每个表中的行数(因为每个表中的每一行都必须与另一个表中的每一行进行比较)、是否实际上有其他过滤器等。不过,循环版本会更糟。如果没有更多信息,无论如何都无能为力。

顺便说一句,使用varchar2而不是char作为对象属性会更正常;这也是rowidtochar()函数返回的数据类型。

相关内容

  • 没有找到相关文章

最新更新