试图将一些 pgpl/SQL 迁移到 psql oracle 函数,我发现了一个我根本无法解释的奇怪行为。
这是我的功能:
CREATE OR REPLACE FUNCTION small_distinct(
tableName IN varchar,
cols IN COL_ARRAY,
whereCond IN varchar
)
RETURN varchar
IS
nbcols PLS_INTEGER;
currentcolnumber PLS_INTEGER;
currentcol varchar(100);
firstcol varchar(100);
sqlpart varchar(4000);
firstquery_str VARCHAR2(4000);
query_str VARCHAR2(4000);
big_query_str VARCHAR2(10000);
firstresult varchar(100);
opt SYS_REFCURSOR;
BEGIN
firstcol:= cols(1);
sqlpart := firstcol;
nbcols := cols.COUNT;
currentcolnumber := 2;
big_query_str := 'SELECT * from ( SELECT '|| sqlpart ||' FROM '||tableName||' WHERE ' || whereCond|| ' ORDER BY '|| firstcol || ') WHERE ROWNUM = 1';
while currentcolnumber <= nbcols loop
sqlpart := sqlpart || ', ' ||cols(currentcolnumber);
currentcolnumber := currentcolnumber +1;
end loop;
EXECUTE IMMEDIATE 'SELECT * from (SELECT '|| firstcol ||' FROM '||tableName ||' WHERE ' || whereCond|| ' ORDER BY '|| firstcol || ') WHERE ROWNUM = 1 ' INTO firstresult;
while firstresult is not null
loop
big_query_str := big_query_str || ' UNION ' || 'SELECT * from ( SELECT '|| sqlpart ||' FROM '||tableName||' WHERE '|| firstcol ||' > '''|| firstresult||''' AND ' || whereCond|| ' ORDER BY '|| firstcol || ') WHERE ROWNUM = 1 ';
EXECUTE IMMEDIATE 'SELECT * from ( SELECT ' || firstcol ||' FROM '||tableName ||' WHERE '|| firstcol ||' > '''|| firstresult||''' AND '|| whereCond|| ' ORDER BY '|| firstcol || ') WHERE ROWNUM = 1' INTO firstresult;
end loop;
return big_query_str;
END small_distinct;
这个函数编译得很好,但是当函数返回时我的 varcharbig_query_str
为空。
正如您在我的函数中看到的那样,这varchar
不可能为空,因为我在第 18 行用一个值对其进行了初始化
我试图删除 so 行以发现问题可能是什么,只有当我删除 while 循环(第 24 行附近)时,返回值才不是 null
有人至少向我解释一下什么样的事件可以重置 PL/SQL 中的 varchar ?