2017-05-06 91 views
1

我想做一个函数,返回一个基于我给的数字的行。我已经创建了该功能,但似乎无法使其工作。我如何执行功能?

这是我的函数:

create or replace function vitest(t_na test.na%type) 
return sys_refcursor is t_test sys_refcursor; 
begin 
open t_test for 
    select * from test where na = t_na; 
return t_test; 
end; 

我已经尝试使用:

select vitest(1) from dual; 

但它给我的错误:ORA-00932。 我使用也试过:

begin 
vitest(1); 
end; 

但它说vitest不是一个过程......

我怎样才能使它发挥作用?

+0

凡test.na%的类型和定义SYS_REFCURSOR? –

+0

@PrescottChartier:'sys_refcursor'是引用游标的标准Oracle类型。 –

+0

我必须在时代背后,我通常定义一个REF CURSOR类型并以此方式使用它。我诚实地向上帝从来没有听说过sys_refcursor直到现在。 –

回答

0

假设你test表有col1col2col3列,你可以打电话给你的功能如下:

DECLARE 
    l_cursor SYS_REFCURSOR; 
    l_col1 test.col1%TYPE; 
    l_col2 test.col2%TYPE; 
    l_col3 test.col3%TYPE; 
BEGIN 

    l_cursor := vitest(1);   
    LOOP 
    FETCH l_cursor 
    INTO l_col1, l_col2, l_col3; 
    EXIT WHEN l_cursor%NOTFOUND; 
    DBMS_OUTPUT.PUT_LINE(l_col1 || ' | ' || l_col2 || ' | ' || l_col3); 
    END LOOP; 
    CLOSE l_cursor; 
END; 

*另外,你选择现在将改变

select * from test where na = t_na;

select col1,col2,col3 from test where na = t_na;