Question

ref cursor and plsql tables ?

Asked by: kandati

I have doubt regarding  ref cursor and plsql tables ?
these are the two topics which i could not get into my mind ? i have read and
searched for many materials and books ? non of them could easily put it into my brain ? can someone help me with  simple explanation with simple example ? how and where these topics could be used ?

actual to begin with, i have no idea of object types  in oracle ?
how could i imagine

for example :-

type xyz as record of tablename%rowtype
type xxx as table  of  tablename indexed by binary_integer


these things make me confuse. please help

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-04-23 at 07:19:19ID20965061
Tags

ref

,

cursor

,

oracle

,

table

Topic

Oracle Database

Participating Experts
2
Points
50
Comments
4

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. plsql
    plsql
  2. plsql
    plsql
  3. sleep command in plsql
    is there sleep command in plsql?
  4. PLSQL protection
    is there a way to stop someone copying PLSQL from one machine to another? Is there something unique about an Oracle database that I can hard code into my PLSQL? thanks
  5. plsql
    can u please send some plsql code to do the follwoing: run a select query for a list of employee_ids (that should be read from my c: drive or any other drive. the employee_id must be read from a csv file in column A).
  6. plsql - unix
    I am strating on a new job tomorow for plsql and unix. I have refeashed plsql concepts , but am not familar with how unix is exactly used by plsql programmers. Please suggest some free tuturial (links) for this - for a plsql developer role so that I can perform well in my new...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: uTabPosted on 2004-04-23 at 10:15:38ID: 10901914



This can be used when you need to return information for a block that requires initial filtering of the information of manipulation of the data in some way that could cause a reduction in system performance if done in a sql statement. For example: you have a block in your form that is based on a table. The user needs to view only a specific rows based upon a dynamic setting.  In other words the user has the option of deciding what they want to filter. These values can be stored in the system and passed to the pkg that sets up the ref_cursor or plsql tables in a when-new-form-instance. If this were all done in the when new form instance it could be quite a load on the system as users increase.  

 

by: n4nazimPosted on 2004-04-23 at 22:34:55ID: 10906321


Go through this... it is lengthy but worth ...


Using a Collection Instead of a Temporary Table in Complex Reports
Compliments of Zlatko Sirotic, (zlatko.sirotic@iii.hr.
Sometimes there is a query in our reports that is so complex that we have to use a temporary table (in an 8i database the most effective type would be a GLOBAL TEMPORARY TABLE). Without such a table, we couldn't do what we intended to, or we could do it but the solution would be overly complicated to design and difficult to maintain. Unfortunately, loading the temporary table (and processing it) may be more time consuming.
Sometimes we can avoid the use of a temporary table by using a REF CURSOR, especially if we use a dynamic REF CURSOR. However, REF CURSORs can't always replace a temporary table. Wouldn't it be nice if we could use a collection instead of temporary tables in our reports?
The reports are not set up to work directly with collections, but we can use collections indirectly in two ways:
·      Our report can be based on a VIEW that uses the collection.
·      Our report can be based on a REF CURSOR that uses the collection.
Here we will discuss the second method, (how to base a report on a REF CURSOR that uses collections), using a TABLE collection. The example is actually so simple that we really don't need a temporary table at all. The goal of this example is to show the method as simply as possible.
The method consists of three steps:
1.      Load a collection with data from one or more tables
 
2.      Execute complex processing on the collection
 
3.      Open a REF CURSOR variable which uses a collection as follows:
4.      OPEN ref_cursor_variable FOR
   SELECT ... FROM TABLE (CAST (collection_variable AS collection_type)) ...
or even simpler (this works only with 9i, and not with 8i):
OPEN ref_cursor_variable FOR
   SELECT ... FROM TABLE (collection_variable) ...
and send the REF CURSOR to the Reports
Even if DEPT is a common relational table (neither an object or object-relational table), we must first create the following OBJECT TYPE:
CREATE OR REPLACE TYPE dept_obj_t AS OBJECT (
   deptno NUMBER (2),
   dname  VARCHAR2 (14)
)
/
Then we can create the following TABLE TYPE:
CREATE OR REPLACE TYPE dept_tab_t AS TABLE OF dept_obj_t
/
Now we can create a package for the report:
CREATE OR REPLACE PACKAGE collection_reports_rc IS
   TYPE dept_rec_t IS RECORD (
      deptno dept.deptno%TYPE,
      dname  dept.dname%TYPE);
   TYPE dept_rc_t IS REF CURSOR RETURN dept_rec_t;
   FUNCTION dept_query RETURN dept_rc_t;
END;
/

CREATE OR REPLACE PACKAGE BODY collection_reports_rc IS
   FUNCTION dept_query RETURN dept_rc_t IS
      l_dept_tab dept_tab_t;
      l_dept_rc  dept_rc_t;
      l_counter  NUMBER := 1;
   BEGIN
      -- ***** step A *****

      -- 1.version: CAST (MULTISET ...
      SELECT CAST (MULTISET (SELECT deptno, dname
                               FROM dept)
                   AS dept_tab_t)
        INTO l_dept_tab
        FROM DUAL;

      -- 2.version: BULK COLLECT
      /*
      SELECT dept_obj_t (deptno, dname)
        BULK COLLECT INTO l_dept_tab
        FROM dept;
      */

      -- 3.version: FOR LOOP
      /*
      l_dept_tab := dept_tab_t ();
      FOR dept IN (
         SELECT deptno, dname
           FROM dept)
      LOOP
         l_dept_tab.EXTEND;
         l_dept_tab (l_counter) := dept_obj_t (dept.deptno, dept.dname);
         l_counter := l_counter + 1;
      END LOOP;
      */

      -- ***** step B (SOME PROCESSING) *****

      -- ***** step C *****

      OPEN l_dept_rc FOR
         SELECT deptno, dname
           FROM TABLE (CAST (l_dept_tab AS dept_tab_t)) -- in 9i works without CAST
          ORDER BY deptno;

      RETURN l_dept_rc;
   END;
END;
/
The previous package used a static REF CURSOR, therefore we couldn't send a dynamic WHERE condition to it. The next package version also uses a static REF CURSOR, but a WHERE condition is being sent to it. The condition is not used in the REF CURSOR, but in the SELECT command which loads the collection with data from the database.
CREATE OR REPLACE PACKAGE collection_reports2_rc IS
   TYPE dept_rec_t IS RECORD (
      deptno dept.deptno%TYPE,
      dname  dept.dname%TYPE);
   TYPE dept_rc_t IS REF CURSOR RETURN dept_rec_t;
   FUNCTION dept_query (p_where VARCHAR2) RETURN dept_rc_t;
END;
/

CREATE OR REPLACE PACKAGE BODY collection_reports2_rc IS
   FUNCTION dept_query (p_where VARCHAR2) RETURN dept_rc_t IS
      l_dept_tab  dept_tab_t;
      l_dept_rc   dept_rc_t;
      l_statement VARCHAR2 (32000);
   BEGIN
      -- ***** step A *****

      -- 1.version: CAST (MULTISET ...
      l_statement :=
      ' SELECT CAST (MULTISET (SELECT deptno, dname
                                 FROM dept
                                WHERE ' || NVL (p_where, '1 = 1') || ')
                     AS dept_tab_t)
          FROM DUAL';

      EXECUTE IMMEDIATE l_statement INTO l_dept_tab;

      -- 2.version: BULK COLLECT (but this don't compile in Oracle 8i)
      /*
      l_statement :=
      ' SELECT dept_obj_t (deptno, dname)
          FROM dept
         WHERE ' || NVL (p_where, '1 = 1');

      EXECUTE IMMEDIATE l_statement BULK COLLECT INTO l_dept_tab;
      */

      -- ***** step B (SOME PROCESSING) *****

      -- ***** step C *****

      OPEN l_dept_rc FOR
         SELECT deptno, dname
           FROM TABLE (CAST (l_dept_tab AS dept_tab_t)) -- in 9i works without CAST
          ORDER BY deptno;

      RETURN l_dept_rc;
   END;
END;
/
If for any reason we'd need a dynamic REF CURSOR, here is a package showing this. In this case, the package must have both static and dynamic REF CURSOR types - static for the Reports and dynamic for the OPEN ref_cursor FOR '...' statement. (See July's Tip of the Month: Dynamic Table in the Second Query with Oracle Reports).
CREATE OR REPLACE PACKAGE collection_reports_dyn_rc IS
   TYPE dept_rec_t IS RECORD (
      deptno dept.deptno%TYPE,
      dname  dept.dname%TYPE);
   TYPE dept_rc_t IS REF CURSOR RETURN dept_rec_t;
   TYPE dept_dyn_rc_t IS REF CURSOR;
   FUNCTION dept_query (p_where VARCHAR2) RETURN dept_dyn_rc_t;
END;
/

CREATE OR REPLACE PACKAGE BODY collection_reports_dyn_rc IS
   FUNCTION dept_query (p_where VARCHAR2) RETURN dept_dyn_rc_t IS
      l_dept_tab dept_tab_t;
      l_dept_rc  dept_dyn_rc_t;
   BEGIN
      -- ***** step A *****

      SELECT CAST (MULTISET (SELECT deptno, dname
                               FROM dept)
                   AS dept_tab_t)
        INTO l_dept_tab
        FROM DUAL;

      -- ***** step B (SOME PROCESSING) *****

      -- ***** step C *****

      OPEN l_dept_rc FOR
         'SELECT deptno, dname
            FROM TABLE (CAST (:l_dept_tab AS dept_tab_t)) -- in 9i works without CAST
           WHERE ' || NVL (p_where, '1 = 1') ||
         ' ORDER BY deptno'
         USING l_dept_tab;

      RETURN l_dept_rc;
   END;
END;
/
We must emphasize that the collection is not always a better solution than the temporary table. Specifically, when a collection must be loaded with a very large amount of data, the collection might be a worse solution as it requires a lot of RAM memory.


Rgds,
Nazim M

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...