Skip to Main Content
  • Questions
  • Call a packaged procedure which has out parameter as collection

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Shamim.

Asked: April 19, 2011 - 12:30 am UTC

Last updated: April 25, 2011 - 7:51 am UTC

Version: 10.2.0

Viewed 1000+ times

You Asked

Tom
I have created a procedure prc_pass_array in a package named pkg_test. The procedure has only one parameter and it is out parameter. The parameter holds the employee id list.

Now when i called that procedure it gives error.
Note: parameter's data type is user defined type.

--the package is
CREATE OR REPLACE PACKAGE pkg_test IS
TYPE v_array IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
PROCEDURE prc_pass_array(p_empid OUT v_array);
END;
/
CREATE OR REPLACE PACKAGE BODY pkg_test IS
PROCEDURE prc_pass_array(p_empid OUT V_ARRAY) IS
BEGIN
SELECT employee_id BULK COLLECT INTO p_empid FROM EMPLOYEES;
END;
END;
/

--and the calling is
DECLARE
TYPE v_array IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
v_empid v_array;

v_emp NUMBER;
BEGIN

PKG_TEST.prc_pass_array(V_empid);
FOR i IN v_empid.first..v_empid.last LOOP
DBMS_OUTPUT.PUT_LINE(v_empid(i));
END LOOP;
END;


--but the following block works
DECLARE
TYPE array_string IS TABLE OF VARCHAR2(50);
v_empid ARRAY_STRING;
BEGIN
SELECT employee_id BULK COLLECT INTO v_empid FROM EMPLOYEES;

FOR i IN v_empid.first..v_empid.last LOOP
DBMS_OUTPUT.PUT_LINE(v_empid(i));
END LOOP;
END;
/


could you please give me the solution(with explanation) for this.

and Tom said...

You created a procedure that takes PKG_TEST.V_ARRAY as an OUT parameter.

You later try to call it using an entirely different type - that type is "anonymous.v_array". To you - they look the same - to the compiler - they are as different as blob and xmltype.

All you need to do is consistently use the right type:
DECLARE 
  v_empid <b>PKG_TEST.</b>v_array; 
  
  v_emp NUMBER; 
BEGIN 
  
  PKG_TEST.prc_pass_array(V_empid); 
  FOR i IN v_empid.first..v_empid.last LOOP 
    DBMS_OUTPUT.PUT_LINE(v_empid(i)); 
  END LOOP; 
END; 

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Problem solved. Thank you very much.

Shamim Ahmed, April 20, 2011 - 10:26 pm UTC

The solution works. And this will help me while working with "collections". Now I have another question. Can I use array in select statement's where clause? If possible, then please give an example. Again thank you very much.
Tom Kyte
April 25, 2011 - 7:51 am UTC

tell me HOW you want to use the 'array' in the where clause.

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here