Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Executing Stored Procedure Using REF CURSOR

Re: Executing Stored Procedure Using REF CURSOR

From: Jan Korecki <Jan.Korecki_at_contactor.se>
Date: Sat, 22 Nov 2003 19:12:19 +0100
Message-ID: <3fbfa738$0$97841$57c3e1d3@news3.bahnhof.se>

> Janne, I really appreciate your response.  Your post did help me
> understand strong typed versus weakly typed cursors, but I'm still
> missing something.  How do you use a strongly typed cursor if the
> query involves a join between two tables? What do I use in place of 
> "all_objects%ROWTYPE"? This is probably a basic PL/SQL statement, but
> I'm a relatively new to PL/SQL so .... I appreciate your patience.
> 
> Again, thanks in advance for your help,
> Mona

Hi!

I'll give you 2 ansvers. A long and a longer example :-) The first one is a working solution for your problem. The other is a revorked example of my last message. If its to much, let that one slide for now.

Since you are new in pl/sql ill recommend that you look up the oracle dokumentation (at http://otn.oracle.com). The "PL/SQL User's Guide and Reference" is a good place to learn pl/sql.

The direct link for the 9i doc is at:
http://download-west.oracle.com/docs/cd/B10501_01/appdev.920/a96624/toc.htm

If you dont have a profile at otn you will be prompted to create one. Its free. Otn is a really greate resource. They have the documentation for older oracle releases too.

Now for the examples:

CREATE OR REPLACE PACKAGE Report_Pkg
AS

END Report_Pkg;
/

CREATE OR REPLACE PACKAGE BODY Report_Pkg AS

END Inv_report_proc;

END Report_Pkg;
/

PL/SQL procedure successfully completed.

Next is a revoked example of my last message. I also added the use of sys_refcursor (new in 9i). You can let this slide if you think its too much. Check the oracle documentation instead.

CREATE OR REPLACE PACKAGE Testrefcur
AS

--weak ref cursor. Can return anything
TYPE all_obj_refcur_type_weak IS REF CURSOR;

PROCEDURE showAllObjects_Strong;
-- I added the p_object_type to show you that you can change the resultset
PROCEDURE showAllObjects_weak(p_object_type IN VARCHAR2 DEFAULT 'ALL_OBJECTS');
PROCEDURE showAllObjects_9i(p_object_type IN VARCHAR2 DEFAULT 'ALL_OBJECTS'); END Testrefcur;
/

CREATE OR REPLACE PACKAGE BODY Testrefcur AS

PROCEDURE getAllObjects_Strong(allObj_refcur OUT all_obj_refcur_type_strong) IS
BEGIN
   OPEN allObj_refcur FOR

              SELECT object_name FROM all_objects WHERE ROWNUM <11;

END getAllObjects_strong;

PROCEDURE getAllObjects_weak(p_object_type IN VARCHAR2, allObj_refcur OUT all_obj_refcur_type_weak)
IS
BEGIN

	 IF (p_object_type ='ALL_TABLES') THEN
   	 	OPEN allObj_refcur FOR
   	   		 SELECT table_name,owner FROM all_tables WHERE ROWNUM <11;
	 ELSE
	 	OPEN allObj_refcur FOR
   	   		 SELECT object_name FROM all_objects WHERE ROWNUM <11;
	 END IF;
	 	

END getAllObjects_weak;

END getAllObjects_9i;

PROCEDURE showAllObjects_strong
IS

rpt_cursor	all_obj_refcur_type_strong;
resultset   rpt_cursor%ROWTYPE;	  --note the differens with the weakly typed

BEGIN
dbms_output.put_line('Strongly');

   getAllObjects_Strong( rpt_cursor);
   FETCH rpt_cursor INTO resultset ;
   WHILE rpt_cursor%FOUND LOOP

   	  dbms_output.put_line(resultset.object_name);
	  FETCH rpt_cursor INTO resultset;

   END LOOP; END showAllObjects_strong;

PROCEDURE showAllObjects_weak(p_object_type IN VARCHAR2 DEFAULT 'ALL_OBJECTS')
IS

rpt_cursor	all_obj_refcur_type_weak;
resultset 	Sample_rec;
resultset2 	Sample_rec2;

BEGIN
dbms_output.put_line('Weakly typed');

   getAllObjects_weak(p_object_type, rpt_cursor);

   IF (p_object_type='ALL_TABLES') THEN

            FETCH rpt_cursor INTO resultset2 ;

   	 WHILE rpt_cursor%FOUND LOOP
   	 	   dbms_output.put_line(resultset2.objectName || ' ' || 
resultset2.objectOwner);
	  	   FETCH rpt_cursor INTO resultset2;
   	 END LOOP;
   ELSE	
      FETCH rpt_cursor INTO resultset ;
   	 WHILE rpt_cursor%FOUND LOOP
   	 	   dbms_output.put_line(resultset.objectName);
	  	   FETCH rpt_cursor INTO resultset;
   	 END LOOP;

   END IF; END showAllObjects_weak;

BEGIN
dbms_output.put_line('Weakly typed');

   getAllObjects_weak(p_object_type, rpt_cursor);

   IF (p_object_type='ALL_TABLES') THEN

            FETCH rpt_cursor INTO resultset2 ;

   	 WHILE rpt_cursor%FOUND LOOP
   	 	   dbms_output.put_line(resultset2.objectName || ' ' || 
resultset2.objectOwner);
	  	   FETCH rpt_cursor INTO resultset2;
   	 END LOOP;
   ELSE	
      FETCH rpt_cursor INTO resultset ;
   	 WHILE rpt_cursor%FOUND LOOP
   	 	   dbms_output.put_line(resultset.objectName);
	  	   FETCH rpt_cursor INTO resultset;
   	 END LOOP;

   END IF; END showAllObjects_9i;

END Testrefcur;
/

Hope it helps and that you understand my swinglish (swedish-english) ;-)

Regards,
Janne! Received on Sat Nov 22 2003 - 12:12:19 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US