如何在输出中调用带有聚合结果的存储过程?

How to call a Stored procedure with aggregate result in output?

我有一个简单的 select 查询需要放入存储过程中。 查询接受 3 个参数并显示 2 列作为结果,其中一列是聚合。

CREATE OR REPLACE PROCEDURE "B_SP_GET_TOTAL_CLOCKED_IN_TIME"
(
     cv_1 IN OUT TYPES.cursorType,
     p_PARENT_CLIENT_ID IN NUMBER DEFAULT 10000,
     p_START_TIME IN NVARCHAR2,
     p_END_TIME IN NVARCHAR2
)
AS
v_sql VARCHAR2(4000);
BEGIN
v_sql := 'SELECT b.CLIENT_NAME, ROUND(SUM((a.ENDTIME-a.STARTTIME)*24*60),2) TOTAL_CLOCKIN_TIME
      FROM TIMESHEET a
      INNER JOIN CLIENTS b ON a.CLIENT_ID = b.CLIENT_ID
      INNER JOIN CLOCKACTIONS c ON c.ID = a.CLOCKACTIONID
      WHERE a.STARTTIME > p_START_TIME AND a.ENDTIME < p_END_TIME AND b.PARENT_CLIENT_ID = p_PARENT_CLIENT_ID 
      GROUP BY b.CLIENT_NAME';    
    OPEN cv_1 FOR v_sql;
END;

我执行了存储过程并且编译成功,没有任何问题。 我如何检查它是否正常工作?至于我现在如何测试它?

我用来测试上述过程的语句可以在下面找到:

execute B_SP_GET_TOTAL_CLOCKED_IN_TIME(10000,'04-01-2015 00:00:00','05-01-2015 00:00:00');

这是我得到的错误:

 Error starting at line : 1 in command - execute B_SP_GET_TOTAL_CLOCKED_IN_TIME(10000,'04-01-2015 00:00:00','05-01-2015 00:00:00') 
Error report - ORA-06550: line 1, column 7: 
PLS-00306: wrong number or types of arguments in call to 'B_SP_GET_TOTAL_CLOCKED_IN_TIME' 
ORA-06550: line 1, column 7: PL/SQL: Statement ignored
    06550. 00000 -  "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    *Action:

不需要(ab)使用动态SQL。您可以简单地使用 OPEN FOR SELECT 并使用 SYS_REFCURSOR.

例如,

SQL> CREATE OR REPLACE
  2  PROCEDURE p_get_emp(
  3      p_deptno IN emp.deptno%TYPE,
  4      p_ref OUT SYS_REFCURSOR)
  5  AS
  6  BEGIN
  7    OPEN   p_ref FOR
  8    SELECT ename,
  9           empno,
 10           deptno
 11    FROM   emp
 12    WHERE  deptno = p_deptno
 13    ORDER BY empno;
 14  END p_get_emp;
 15  /

Procedure created.

SQL>
SQL> sho err
No errors.
SQL>

创建的过程没有任何错误。让我们测试一下:

SQL> var p_ref refcursor
SQL>
SQL> EXEC p_get_emp  (30, :p_ref);

PL/SQL procedure successfully completed.

SQL>
SQL> print p_ref

ENAME           EMPNO     DEPTNO
---------- ---------- ----------
ALLEN            7499         30
WARD             7521         30
MARTIN           7654         30
BLAKE            7698         30
TURNER           7844         30
JAMES            7900         30

6 rows selected.

SQL>