0
create or replace FUNCTION RETURN_AP_ORDER_CUR (count_product OUT NOCOPY SYS_REFCURSOR)
RETURN SYS_REFCURSOR 
AS
APP_RECORDSET SYS_REFCURSOR;
i AP_ORDER%ROWTYPE;

BEGIN 

OPEN APP_RECORDSET FOR 
SELECT ORDER_ID,REGEXP_REPLACE(PRODUCT_NAME, '^\s', '') PRODUCT_NAME
   FROM
        (
         SELECT a.ORDER_ID, b.COLUMN_VALUE as PRODUCT_NAME
         FROM (SELECT ORDER_ID, PRODUCT_NAME FROM AP_ORDER) a 
         NATURAL JOIN TABLE(FNC_COL2ROW(PRODUCT_NAME)) b 
        );

RETURN APP_RECORDSET;

LOOP
 FETCH APP_RECORDSET INTO i;
EXIT WHEN APP_RECORDSET%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(i.ORDER_ID||' '||i.PRODUCT_NAME);
INSERT INTO AP_ORDER_SPLIT (ORDER_ID,PRODUCT_NAME) VALUES (i.ORDER_ID,i.PRODUCT_NAME);
COMMIT;
END LOOP;
close APP_RECORDSET;

OPEN count_product FOR Select ORDER_ID,COUNT(1) from AP_ORDER_SPLIT group by ORDER_ID;
   
    EXCEPTION
WHEN TOO_MANY_ROWS THEN
 dbms_output.put_line('**Error** Several Values'); -- do something meaningful here 
WHEN NO_DATA_FOUND THEN
 dbms_output.put_line('There is no data in variable');
WHEN OTHERS THEN
  dbms_output.put_line('Error code ' || SQLCODE || SQLERRM);
   END RETURN_AP_ORDER_CUR;

使用绑定变量如何在 SQL 开发人员中获取输出参数和返回值的结果。

请分享我一个匿名块请提出一些方法

  • 将逗号分隔值存储到 ref-cursor
  • 在sql语句中使用函数来获取分隔值
  • 获取计数数据到输出参数
4

1 回答 1

0

一旦函数到达RETURN语句,就不会执行任何其他代码,因此所有代码(循环、获取等)都被“跳过”。

一个简化的说明如何做到这一点:

SQL> create or replace function f_test (par_rc out nocopy sys_refcursor)
  2    return sys_refcursor
  3  is
  4    l_rc sys_refcursor;
  5  begin
  6    open par_rc for select * from dept;
  7
  8    open l_rc for select empno, ename, job, sal from emp
  9                  where deptno = 10;
 10
 11    return l_rc;
 12  end;
 13  /

Function created.

调用这样的函数:

SQL> declare
  2    l_out sys_refcursor;  -- will hold function's OUT parameter
  3    l_ret sys_refcursor;  -- will hold function's RETURN value
  4  begin
  5    l_ret := f_test(l_out);
  6  end;
  7  /

PL/SQL procedure successfully completed.

SQL>
于 2020-06-23T17:27:23.723 回答