我尝试创建一个Oracle函数,其中表名,列名和where字符串是动态参数:
CREATE OR REPLACE FUNCTION MYSCHEMA.myFunctionName(
tableName in nvarchar2,
columnName in nvarchar2,
whereStr in nvarchar2)
RETURN nvarchar2
IS nActive nvarchar2(2000);
BEGIN
declare
querystr nvarchar2(2000) ;
result nvarchar2(2000);
begin
querystr :='
select listagg('+columnName+','+','+') within group (order by '+columnName+')
from '+tableName+' where 1 = 1 '+whereStr+';';
EXECUTE IMMEDIATE querystr
INTO result;
nActive := result;
RETURN ( nActive );
end;
END ;
/
但它给了我错误“警告:编译但编译错误”。
我做错了什么?
答案 0 :(得分:3)
Oracle
中的联接字符串,使用||
而非+
;
查询字符串execute immediate
'
转义''
。......正如@Aleksej所说
Execute immediate
需要CHAR or VARCHAR2
; listagg
返回raw
或VARCHAR2
CREATE OR REPLACE FUNCTION MYSCHEMA.myFunctionName(
tableName in varchar2,
columnName in varchar2,
whereStr in varchar2)
RETURN varchar2
BEGIN
declare
querystr varchar2(2000) ;
result varchar2(2000);
begin
querystr :='
select listagg('|| columnName || ', '','') within group (order by ' ||columnName ||')
from ' || tableName || ' where 1 = 1 ' || whereStr;
EXECUTE IMMEDIATE querystr INTO result;
return result;
end;
END ;
/
答案 1 :(得分:1)
考虑到
,您需要更改变量的类型所以:
declare
querystr varchar2(2000) ;
result VARCHAR2(32767);
begin
...