Oracle在存储过程中的IF语句中选择

时间:2015-10-27 15:51:33

标签: oracle

来自sql server后台,我试图掌握oracle语法。我试图从存储过程返回一些记录,但收到错误:

CREATE OR REPLACE PROCEDURE SP_GetCustomers(username IN VARCHAR2)
AS

BEGIN

    if username = 'all' then
        select *
        from customers c;
    else
        select *
        from customers c
        where c.created_by = username;
    end if;

END;

我错过了什么?

2 个答案:

答案 0 :(得分:0)

您缺少INTO子句,但在此阶段最好使用FUNCTION返回值,使用PROCEDURE执行操作。

答案 1 :(得分:0)

ORACLE不会将隐式游标创建为返回参数,您必须显式声明引用游标并将其打开以返回指向游标的指针,如:

CREATE OR REPLACE Function SP_GetCustomers(username IN VARCHAR2) return sys_refcursor
AS
   ret_cursor sys_refcursor;
BEGIN

    if username = 'all' then
        open ret_cursor for
        'select *
        from customers c';
    else
        open ret_cursor for
        'select *
        from customers c
        where c.created_by = :username' using username ;
    end if;
  return ret_cursor;
END;