问题
create or replace
FUNCTION test_fun (
p_ref_cur OUT SYS_REFCURSOR,
p_a_code IN NUMBER DEFAULT 0,
p_category IN package.category%TYPE DEFAULT NULL,
p_name IN package.name%TYPE DEFAULT NULL,
p_display_name IN package.display_name%TYPE DEFAULT NULL,
p_rowid IN package."rowid"%TYPE DEFAULT NULL,
p_flg IN package.flg%TYPE DEFAULT '1',
p_mod_dat IN package.mod_dat%TYPE DEFAULT SYSTIMESTAMP,
p_mod_usr IN package.mod_usr%TYPE DEFAULT NULL
)
RETURN NUMBER
AS
How to call this function in oracle which has SYS_REFCURSOR as OUT parameter?
Update: **test_fun** will be called from another function say caller() where the complete function to be executed will be taken from the database and execute using execute immediate command.
SELECT command into cmd from data_table where id=p_id;
execute immediate (cmd);
cmd value will be like
test_fun(v_cv1,0, 'pp', 'np123', 'np', NULL, 1, NULL, 'testuser');
NOTE: We have control over the caller() function and cmd to be execute but no control over test_fun()
Any help is much appreciated.
回答1:
Just declare a variable of type SYS_REFCURSOR in the calling block, and call your function, e.g. from SQL/Plus:
set autoprint on;
var cur refcursor;
declare
FUNCTION test_fun (
p_ref_cur OUT SYS_REFCURSOR)
RETURN NUMBER is
begin
open p_ref_cur for select * from dual;
return 1;
end;
begin
dbms_output.put_line(test_fun(:cur));
end;
/
回答2:
This procedure did the magic
PROCEDURE executecmd(cmd_in IN VARCHAR2)
AS
v_cv1 SYS_REFCURSOR;
BEGIN
dbms_output.put_line('Executing cmd: '|| cmd);
execute immediate 'declare
result number;
BEGIN
result:='||cmd_in||';'||
'END;'
USING v_cv1;
END;
来源:https://stackoverflow.com/questions/13579672/how-to-call-oracle-function-which-has-sys-refcursor-as-out-parameter