0

I'm struggling with a variable argument stored procedure that has to perform a SELECT on a table using every argument passed to it in its WHERE clause.

Basically I have N account numbers as parameter and I want to return a table with the result of selecting three fields for each account number.

This is what I've done so far:

function sp_get_minutes_expiration_default(retval IN OUT char, gc IN OUT GenericCursor,
        p_account_num IN CLIENT_ACCOUNTS.ACCOUNT_NUM%TYPE) return number
        is
    r_cod integer := 0;
    begin
        open gc for select account_num, concept_def, minutes_expiration_def from CLIENT_ACCOUNTS
            where p_account_num = account_num;  -- MAYBE A FOR LOOP HERE?
        return r_cod;
    exception

        -- EXCEPTION HANDLING

    end sp_get_minutes_expiration_default;

My brute force solution would be to maybe loop over a list of account numbers, select and maybe do a UNION or append to the result table?

1 Answer 1

1

If you cast your input parameter as a table, then you can join it to CLIENT_ACCOUNTS

select account_num, concept_def, minutes_expiration_def
from CLIENT_ACCOUNTS ca, table(p_account_num) a
where a.account_num = ca.account_num

But I would recommend you select the output into another collection that is the output of the function (or procedure). I would urge you to not use reference cursors.

ADDENDUM 1

A more complete example follows:

create or replace type id_type_array as table of number;
/

declare
   ti id_type_array := id_type_array();
   n number;
begin
   ti.extend();
   ti(1) := 42;
   select column_value into n from table(ti) where rownum = 1;
end;
/

In your code, you would need to use the framework's API to:

  1. create an instance of the collection (of type id_type_array)
  2. populate the collection with the list of numbers
  3. Execute the anonymous PL/SQL block, binding in the collection

But you should immediately see that you don't have to put the query into an anonymous PL/SQL block to execute it (even though many experienced Oracle developers advocate it). You can execute the query just like any other query so long as you bind the correct parameter:

select account_num, concept_def, minutes_expiration_def
from CLIENT_ACCOUNTS ca, table(:p_account_num) a
where a.column_value = ca.account_num
Sign up to request clarification or add additional context in comments.

2 Comments

@jeff5times7 In this short example p_account_num should be of what type, a SQL LIST?? I've been reading about the TABLE() function but I don't really understand what argument it takes. Could you expand a little further please?
Please see ADDENDUM 1.

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.