0

I have a PL/SQL package where i want to declare a select-statment which are used by different other Packages. So i see to ways. First way i define a cursor which can be called from other packages and store the select. Second way would be a procedure which stored the select.

Can someone tell me the advantages and disadvantages of each way? My Prof. say Cursor are old and statefull and noone use this today. My Chef tell me Cursor is faster to iterate and you can make Types of it.

Can someone tell me what's the best practice here?

For example:

CURSOR crs_active_customer IS
 SELECT * FROM customer where status = 'active'

OR

PROCEDURE prc_getActiveCustomer IS
BEGIN
 SELECT * FROM customer where status = 'active';
END prc_getActiveCustomer;

What is better way to store select-statements.

6
  • 3
    Find a better professor.
    – booyaa
    Sep 2, 2015 at 8:09
  • It's easy to get mixed up in the terminology. To make sure we understand your question, it would be useful if you gave sample SQL, PL/SQL to illustrate both options you are trying to compare.
    – sstan
    Sep 2, 2015 at 12:53
  • @sstan I update the question
    – Sebi
    Sep 3, 2015 at 9:42
  • 1
    I'll second the "new professor" advice, especially if he/she condones prefixing cursors and procedures with crs_ and prc_. Sep 3, 2015 at 20:14

2 Answers 2

1

I would write a function that returns a new cursor instance every time you call it. A cursor variable in a package is actually a GLOBAL variable: you can have only one procedure at a time using it. This is probably the problem your professor is referring to.

Having a global cursor means that you will run into "cursor already open" errors if you write a a procedure that, while scanning the results of such cursor calls another function that internally needs to use the same cursor.

0

PL/SQL 101 to the rescue ! From Working with Cursors:

The central purpose of the Oracle PL/SQL language is to make it as easy and efficient as possible to query and change the contents of tables in a database. You must, of course, use the SQL language to access tables, and each time you do so, you use a cursor to get the job done.

So every time you have SQL in PL/SQL there will be a cursor. The next question is what kinds of cursors there is and when to use them. The above mentioned article touches also this topic.

You can also read the fine manual: Cursors

A cursor is a pointer to a private SQL area that stores information about processing a specific SELECT or DML statement.

And then carry on reading about implicit and explicit cursors.

Next find a better professor.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.