0

I have tables(Customers,Pbasket and pp) with the following values inserted to the table

http://pastebin.com/eMUtLJn9

Basically I am tasked to create a function that finds all the product number(p#) purchased by the customer and I have to pass in the customer id(c#) as a input parameter;

This is my pl/sql script

http://pastebin.com/SqkY0P9N

I noticed that

there is no results returned for c#(100), which is correct.

but i noticed that for c#(101) and c#(102)

the result should return more than one p# but it only returns 1 result even though I had my while loop.

the output of my results

enter image description here

Please advice.

1 Answer 1

1

The function you wrote was supposed to return One row only. And also, you had a return in your LOOP. So after first iteration the control is returned back.(leaving the cursor opened for ever)

Create a TYPE of SQL Object

create type my_numbers as table of NUMBER;
/

FUNCTION returning a table!

CREATE OR REPLACE FUNCTION  purchased(cId IN NUMBER) 
RETURN my_numbers
IS  
    product VARCHAR2(45); 
    I NUMBER;
    v_my_list my_numbers := my_numbers();
    CURSOR CursorRow IS 
    SELECT p# 
    FROM customer c  
    LEFT OUTER JOIN pbasket pb 
    ON c.c# = pb.c# 
    LEFT OUTER JOIN pp pp 
    ON pb.whenfinalised = pp.whenfinalised
    WHERE c.c# = cId;
    CurrentPos CursorRow%ROWTYPE; 
BEGIN
    OPEN CursorRow;  
    I := 1;
    FETCH CursorRow into CurrentPos;
    LOOP
    EXIT WHEN CursorRow%NOTFOUND    
       v_my_list.EXTEND;
       v_my_list(I) := CurrentPos.p#;   
       I := I + 1;
    END LOOP;
    CLOSE CursorRow;
    RETURN v_my_list;
END purchased;
/

Final SELECT Query:

select  * FROM TABLE(CAST(purchased(100) as my_numbers));

We can also use Pipelined functions (Slight modification needed) for performance over Large resultsets!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.