从IF语句中的PL / SQL函数返回多个值

时间:2016-06-03 11:23:29

标签: oracle plsql

嗨 - 我正在尝试让我的函数返回select语句中的所有(8)值。根据我在SO上发现的内容尝试了几个变体,但只能让它返回单个值而不是全部8.新的PL / SQL和第一个发布的SO - 我在MSSQL中有相同的运行但是完全不同在PL / SQL中。任何帮助表示赞赏

###尝试1 - 仅返回第一个值

create or replace FUNCTION "SP_SETGRIPSTAGE" (gripNoIn NUMBER, docNoIn NUMBER)
    RETURN VARCHAR2

IS

    PW_GRIP NUMBER := gripNoIn;
    PW_DOCID NUMBER := docNoIn;
    resultOut VARCHAR2(1);

    CURSOR c1

IS
    SELECT pw_code FROM ENV_LKP_GRIP ORDER BY pw_code;

BEGIN

IF PW_DOCID > 0 THEN
    RETURN PW_GRIP;
ELSE
    OPEN c1;
    FETCH c1 into resultOut;
END IF;

    CLOSE c1;
    RETURN resultOut;

END SP_SETGRIPSTAGE;

###尝试2 - 仅返回最后一个值

create or replace FUNCTION "SP_SETGRIPSTAGE" (gripNoIn NUMBER, docNoIn NUMBER)
    RETURN VARCHAR2

IS

    PW_GRIP NUMBER := gripNoIn;
    PW_DOCID NUMBER := docNoIn;
    resultOut VARCHAR2(1);

    CURSOR c1

IS
    SELECT pw_code FROM ENV_LKP_GRIP ORDER BY pw_code;

BEGIN

IF PW_DOCID > 0 THEN
    RETURN PW_GRIP;
ELSE
    OPEN c1;
    LOOP
    FETCH c1 into resultOut;
    EXIT WHEN c1%NOTFOUND;
    END LOOP;
    RETURN resultOut;
END IF;

    CLOSE c1;

END SP_SETGRIPSTAGE;

1 个答案:

答案 0 :(得分:4)

返回光标:

create or replace FUNCTION SP_SETGRIPSTAGE (
  gripNoIn NUMBER,
  docNoIn NUMBER
) RETURN SYS_REFCURSOR
IS
   resultOut SYS_REFCURSOR;
BEGIN
  IF docNoIn > 0 THEN
    OPEN resultOut FOR
    SELECT   pw_code
    FROM     ENV_LKP_GRIP
    ORDER BY pw_code;
  ELSE
    OPEN resultOut FOR
    SELECT   gripNoIn AS pw_code
    FROM     DUAL;
  END IF;

  RETURN resultOut;
END SP_SETGRIPSTAGE;
/

或集合:

create or replace FUNCTION SP_SETGRIPSTAGE (
  gripNoIn NUMBER,
  docNoIn NUMBER
) RETURN SYS.ODCIVARCHAR2LIST
IS
   resultOut SYS.ODCIVARCHAR2LIST;
BEGIN
  IF docNoIn > 0 THEN
    SELECT   pw_code
    BULK COLLECT INTO resultOut
    FROM     ENV_LKP_GRIP
    ORDER BY pw_code;
  ELSE
    resultOut := SYS.ODCIVARCHAR2LIST( gripNoIn );
  END IF;

  RETURN resultOut;
END SP_SETGRIPSTAGE;
/