TOAD 显示存储过程返回的游标记录集

Posted

技术标签:

【中文标题】TOAD 显示存储过程返回的游标记录集【英文标题】:TOAD displaying cursor recordset returned by stored procedure 【发布时间】:2013-01-13 17:05:19 【问题描述】:

请问如何从返回的光标中打印重新设置结果。 下面执行正常,但我需要看到结果。

这是 TOAD 中的块,调用包 sp AMD_NEEDMSG:

DECLARE 
     RETURN_RECORDSET CTI_MATRIX.AMD.REF_CURSOR;
    BEGIN 
     CTI_MATRIX.AMD.AMD_NEEDMSG ( '88888888885', RETURN_RECORDSET );
END;

此包装规格:

    CREATE OR REPLACE PACKAGE CTI_MATRIX.AMD AS
      TYPE REF_CURSOR IS REF CURSOR;
      PROCEDURE AMD_NEEDMSG (v_CRN IN VARCHAR2, return_recordset OUT REF_CURSOR);
    END AMD;

这是包体:

CREATE OR REPLACE PACKAGE BODY CTI_MATRIX.AMD AS

PROCEDURE AMD_NEEDMSG (v_CRN IN VARCHAR2, return_recordset OUT REF_CURSOR) IS


return_flag INTEGER;
row_cnt INTEGER;
number_of_days INTEGER;
var_DATE DATE;

CURSOR ACCNTSEARCH (P_CRN IN VARCHAR2) IS
    SELECT DISTINCT COUNT(*) , AMD.MSG_DATE    
       FROM TBL_AMD_NEEDMSG AMD
      WHERE AMD.PHONE_NUMBER = P_CRN AND ROWNUM = 1;

BEGIN 
 OPEN ACCNTSEARCH(v_CRN);
 FETCH ACCNTSEARCH INTO row_cnt, var_DATE;
 CLOSE ACCNTSEARCH;

 IF (row_cnt = 0)
 THEN
        INSERT INTO TBL_AMD_NEEDMSG (PHONE_NUMBER, MSG_DATE) VALUES (v_CRN , SYSDATE); 
        return_flag := 1;
 ELSE
    SELECT SYSDATE-var_DATE INTO number_of_days FROM dual;

     IF (number_of_days>7)
     THEN 
        UPDATE TBL_AMD_NEEDMSG SET MSG_DATE = SYSDATE WHERE PHONE_NUMBER = v_CRN;
        return_flag := 1;
    ELSE
         return_flag := 0;
    END IF;

 END IF;    

 COMMIT;

 OPEN return_recordset FOR 
 SELECT return_flag AS ReturnFLag FROM DUAL;

EXCEPTION
 WHEN OTHERS THEN
    ROLLBACK;

END AMD_NEEDMSG;

END AMD;
/

底线是以记录集的形式向客户端返回一个return_flag的值。

【问题讨论】:

【参考方案1】:

要返回返回标志,即 int=1 或 0,您需要返回单个值而不是记录集的函数。以下是一般记录集示例 - 希望对您有所帮助:

DECLARE
  TYPE empcurtyp IS REF CURSOR;
  emp_cv  empcurtyp;
--
  TYPE namelist IS TABLE OF scott.emp.ename%TYPE;
  TYPE sallist  IS TABLE OF scott.emp.sal%TYPE;
  names   namelist;
  sals    sallist;
BEGIN
  OPEN emp_cv FOR
    SELECT ename, sal FROM scott.emp
      WHERE job = 'MANAGER' ORDER BY sal DESC;
--
  FETCH emp_cv BULK COLLECT INTO names, sals;
  CLOSE emp_cv;

-- loop through the names and sals collections
 FOR i IN names.FIRST .. names.LAST LOOP
   DBMS_OUTPUT.PUT_LINE
     ('Name = ' || names(i) || ', salary = ' || sals(i));
   END LOOP;
END;
/

-- SYS_REFCURSOR example --
DECLARE
  p_rc_type  SYS_REFCURSOR;
  emp_rec    scott.emp%ROWTYPE;
 --
 PROCEDURE p_Emp_Info (p_cur_var OUT SYS_REFCURSOR)
 IS 
 BEGIN
    OPEN p_cur_var FOR 
     SELECT ename, job FROM scott.emp WHERE job = 'MANAGER';
   LOOP
    FETCH p_cur_var INTO emp_rec.ename, emp_rec.job;
    EXIT WHEN p_cur_var%NOTFOUND;
     dbms_output.put_line(emp_rec.ename ||' '|| emp_rec.job);
   END LOOP;
  CLOSE p_cur_var;
 END p_Emp_Info;
 --
 BEGIN
  p_Emp_Info(p_rc_type);
END;
/

【讨论】:

以上是关于TOAD 显示存储过程返回的游标记录集的主要内容,如果未能解决你的问题,请参考以下文章

C#显示存储过程返回的游标数据集

C#显示存储过程返回的游标数据集

oracle 存储过程执行动态SQL 返回结果给游标,外部程序获得dataset结果集。

oracle如何执行存储过程以及如何返回一个table

oracle 如何返回多条记录

MySQL 存储过程,获取使用游标查询的结果集