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

时间:2013-01-13 17:05:19

标签: plsql procedure toad

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

这是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 个答案:

答案 0 :(得分:0)

要返回return_flag,即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;
/