Oracle UDF问题

时间:2017-11-29 19:15:06

标签: oracle plsql user-defined-functions

我是Oracle UDF的新手。我有一个类似于下面的Oracle UDF UDF_DoStuff,它调用基于C的UDF myUDFs.doStuff

创建或替换

FUNCTION UDF_DoStuff (
    str1 IN VARCHAR2,
    str2 IN VARCHAR2
)
RETURN VARCHAR2 AS output VARCHAR2(50);
BEGIN
   output:= CASE
    WHEN str2 = 'x' THEN myUDFs.doStuff(str1,'a')
    WHEN str2 = 'y' THEN myUDFs.doStuff(str1,'b')
    ELSE null
END;
RETURN output;   
END;

我正在尝试修改它以检索运行查询的用户名并将其传递给myUDFs.doStuff

create or replace 
FUNCTION UDF_DoStuff (
    str1 IN VARCHAR2,
    str2 IN VARCHAR2
)
RETURN VARCHAR2 AS output VARCHAR2(50);
uname VARCHAR2(50);
BEGIN
   select SYS_CONTEXT('USERENV', 'SESSION_USER') into uname from dual;
   output:= CASE
    WHEN str2 = 'x' THEN myUDFs.doStuff(str1,'a', uname)
    WHEN str2 = 'y' THEN myUDFs.doStuff(str1,'b', uname)
    ELSE null
END;
RETURN output;   
END;

但是,我在声明output:= CASE附近收到以下错误。有什么想法,我怎么能让它工作?

Error(10,4): PL/SQL: Statement ignored

1 个答案:

答案 0 :(得分:0)

使用这样的代替:

select SYS_CONTEXT('USERENV', 'SESSION_USER'),
   CASE
    WHEN str2 = 'x' THEN myUDFs.doStuff(str1,'a', SYS_CONTEXT('USERENV', 'SESSION_USER')) 
    WHEN str2 = 'y' THEN myUDFs.doStuff(str1,'b', SYS_CONTEXT('USERENV', 'SESSION_USER')) 
    ELSE null
    END
into uname, output
from dual;
在sql中使用

Case-When语句,以提供if-then pl/sql语句的等效语句。