如何在Oracle SQL中将多个行值返回到一个游标变量?

时间:2014-10-14 16:55:35

标签: sql database oracle cursor

我在oracle中有一个包含一个存储过程的包。它将日期作为输入,将数字作为输入,并将数据输出到ref_cursor。存储过程的目的是提取所有"中奖号码"对应于某个输入日期。

CREATE OR REPLACE PACKAGE GETWINNINGNUMBERSBYDATEPKG IS

TYPE lotgwndate_ref_cursor IS REF CURSOR;     
PROCEDURE GetWinningNumbersDATE (lg_wndate IN date, lg_wnwgid IN number, lg_ref OUT 
lotgwndate_ref_cursor); 

END GETWINNINGNUMBERSBYDATEPKG;    

这是包体。同样,这里的目的是提取所有"中奖号码"对应于特定日期和" gameid"数。然后,我想配对#34;中奖号码"使用"游戏名称"和"抽奖日期"。 (这是从彩票图纸中提取记录)

  PROCEDURE GetWinningNumbersDATE (lg_wndate IN date, lg_wnwgid IN number, lg_ref OUT    
  lotgwndate_ref_cursor) IS
   BEGIN
    OPEN lg_ref FOR
      SELECT a.GAMENAME, b.DRAWINGDATE, c.BALLNUMBER
        FROM GAMEDETAILS a
          INNER JOIN WINNINGRECORDS b
            on a.GAMEDETAILSID = b.GAMEDETAILSID 
              INNER JOIN WINNINGBALLS c
                on b.WINNINGRECORDSID = c.WINNINGRECORDSID


      WHERE b.DRAWINGDATE = lg_wndate
      AND a.GAMEDETAILSID = lg_wnwgid;

END GetWinningNumbersDATE;

END GETWINNINGNUMBERSBYDATEPKG;

这是通话程序。

SET SERVEROUTPUT ON size 100000
DECLARE 
  v_cursor               LOTTERYGAMEPKG.lotgwndate_ref_cursor;
  v_gamename             GAMEDETAILS.gamename%type;
  v_drawingdate          WINNINGRECORDS.drawingdate%type;
  v_ballnumber           WINNINGBALLS.ballnumber%type;
BEGIN
  GETWINNINGNUMBERSBYDATEPKG.GetWinningNumbersDATE(lg_wndate =>    
TO_DATE('06/08/2014','dd/mm/yyyy'), lg_wnwgid => '4', /*date/gamedetailsid #(1-4)*/
                          lg_ref => v_cursor);
LOOP                             
  FETCH v_cursor
    INTO  v_gamename, v_drawingdate, v_ballnumber;
    EXIT WHEN v_cursor%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE
    (v_gamename || ',' || v_drawingdate || ',' || v_ballnumber);
END LOOP;
 END;

这有效,并返回:

NorthstarCash,06-AUG-14,10
NorthstarCash,06-AUG-14,11
NorthstarCash,06-AUG-14,15
NorthstarCash,06-AUG-14,16
NorthstarCash,06-AUG-14,21

但是,我希望它返回

NorthstarCash,06-AUG-14,10,11,15,16,21

有没有办法做到这一点?我已经看了很多其他问题,但没有一个问题和我的问题一样。 ORACLE 11g

1 个答案:

答案 0 :(得分:0)

如果您使用Oracle 11g,只需使用LISTAGG修改SELECT查询

PROCEDURE GetWinningNumbersDATE (lg_wndate IN date, lg_wnwgid IN number, lg_ref OUT    
  lotgwndate_ref_cursor) IS
   BEGIN
    OPEN lg_ref FOR
      SELECT a.GAMENAME, b.DRAWINGDATE, LISTAGG(c.BALLNUMBER,',') WITHIN GROUP (ORDER BY c.BALLNUMBER) AS BALLNUMBER
        FROM GAMEDETAILS a
          INNER JOIN WINNINGRECORDS b
            on a.GAMEDETAILSID = b.GAMEDETAILSID 
              INNER JOIN WINNINGBALLS c
                on b.WINNINGRECORDSID = c.WINNINGRECORDSID  
        WHERE b.DRAWINGDATE = lg_wndate
      AND a.GAMEDETAILSID = lg_wnwgid
     GROUP BY a.GAMENAME, b.DRAWINGDATE;

END GetWinningNumbersDATE;

调用者必须像下面那样进行修改。

v_ballnumber           VARCHAR2(4000);