create or replace function get_last_5_rounds(i_party_id in number) return SYS_REFCURSOR as
resault_set SYS_REFCURSOR;
v_round VAR_ROUND:=VAR_ROUND();
begin
SELECT round
BULK COLLECT INTO v_round
FROM (SELECT DISTINCT session_id,
ROUND
FROM super_six_tickets
where party_id = i_party_id
ORDER BY session_id DESC
)
WHERE ROWNUM <= 5;
OPEN RESAULT_SET for
select rp.session_id, s.symbol_name_in_number ball_number,
rp.position ,
rp.additional_symbol
from rp_deck rp,
symbols s
where session_id MEMBER OF v_round
and s.game_name_id in 38
and s.id = rp.card_name_id
and s.client_id = 1
and rp.position < 36
order by rp.position ;
RETURN RESAULT_SET;
end get_last_5_rounds;
I have a function which will return ball_number, position from last 5 rounds (p_round).
In first select I get 5 rounds, but I am also getting error on the second select:
an INTO clause is expected in this select
How will I pass all rounds from first select and separate them with ,
(comma) to include them IN clause ?
Thanks!
EDITED:
Now i m getting this:
I need to separate all positions and ball number for unique session ID. Now i am getting sorted by position . How can i do that?
In first select I get 5 rounds, but I am also getting error on the second select:
Even your first select will throw error since you are trying to put multiple rows in a single dimension variable. In your case either you need to loop
and and populate your variables or you need to create a collection
and do bulk
operation to fit all the rows.
How will I pass all rounds from first select and separate them with , (comma) to include them IN clause ?
You would need a nested
table to achieve your requirement. See below the working code and read inline explaination for understanding.
Tables:
CREATE TABLE super_six_tickets(session_id NUMBER,ROUND NUMBER);
/
CREATE TABLE rp_deck (session_id NUMBER, position NUMBER,additional_symbol VARCHAR2(10));
/
CREATE TABLE symbols(symbol_name_in_number NUMBER);
/
--Create a type of number to hold the result of you first query.
CREATE TYPE VAR_ROUND IS TABLE OF NUMBER;
/
Function:
CREATE OR REPLACE FUNCTION get_last_5_rounds
RETURN SYS_REFCURSOR
AS
resault_set SYS_REFCURSOR;
--Initialization of varaible of nested table type
v_round VAR_ROUND:=VAR_ROUND();
BEGIN
SELECT round
BULK COLLECT INTO v_round --<--Fetching the rounds information in the variable
FROM
(SELECT DISTINCT session_id,
ROUND
FROM super_six_tickets
ORDER BY session_id DESC
)
WHERE ROWNUM <= 5;
--Opening Sys_refcursor to get the result.
OPEN RESAULT_SET for
SELECT s.symbol_name_in_number ball_number,
rp.position,
rp.additional_symbol
FROM rp_deck rp,
symbols s
WHERE rp.session_id MEMBER OF v_round --<-- checking In clause.
-- You can use this as well. However `MEMBER OF` clause is provided by Oracle to handle such situations.
--> rp.session_id in (Select column_value from table(v_round))
ORDER BY rp.position ASC;
RETURN RESAULT_SET;
END get_last_5_rounds;