Ref Cursors

What is the main use of Ref cursors? when should I go for it(With no other alternative)?
pms_reddy1Asked:
Who is Participating?
 
slightwv (䄆 Netminder) Commented:
OK, I found the two web references where you copied and pasted that from.  Is that supposed to mean something to me?

They are also, opinions...

I stand by my comments:  Use the right tool for the right job.

You cannot say: Give me the 12th row from a cursor.  You can from a collection.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
http://www.oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets.php

A ref_cursor is a reference to a cursor in a stored procedure or function.  Essentialy it is used as a parameter type when you need to pass the cursor around.
0
 
slightwv (䄆 Netminder) Commented:
In addition to the above:

A cursor is nothing more than a result-set.  Every select you run creates a cursor to present the results.

Alternatives are anything that is a 'collection'.  In Oracle you could use a PL/SQL table (not a normal databsae table) or a VARRAY:

http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/composites.htm#CIHIEBJC

The difference with a cursor over the other collection types is it is one-way one-time processing.  You move through the records forward only and only once.  You re-process the results, you would need to basically reopen the cursor by reexecuting the select statement that generated it.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
slightwv (䄆 Netminder) Commented:
Might I ask why this was given a C grade?  If you were in need of additional information you should ask.

Please review:
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

If you do not respond, I'll ask a Moderator to review the grade on this question.
0
 
pms_reddy1Author Commented:
Hi slightwv,

When should we use Ref & when should we use collection types in terms of memory/performance, Could you please provide some examples.

Thanks,
0
 
slightwv (䄆 Netminder) Commented:
Unfortunately this is a different question.

The quick answer is basically what I posted in your question about collections:
http://www.experts-exchange.com/Database/Oracle/Q_27673026.html

Use the proper tool for the proper job.  Cursors are probably the easiest to pass around result sets between processes.  As long as you do not need the results to persist.

If you need the results to persist or need to move through them more than once or backwards, then a collection is likely the best.

As far as examples go, I'm not sure what you are looking for that isn't availabe in the docs that talk about the different types.

A simple cursor example using sqlplus is:


var mycur refcursor

begin
open :mycur for 'select sysdate from dual';
end;
/

print mycur
0
 
pms_reddy1Author Commented:
In my main question I asked "when should I go for it(With no other alternative)?"
I did not find the answer for my question in your replay, Insted you mentioned the alternative as collections.
Hi slightwv,

If you understand my question properly ,I am asking is there any situvation like "we have to opt for the ref cursors, No other way" , Atleast you did not read my question properly, That's why I have given the grade as C.

Please respond If you know the answer,Don't look for the points/Grade, If the reader satisfy with your answer obviously you will get the points/Grade A, No need to demand.

Thanks,
0
 
slightwv (䄆 Netminder) Commented:
99% of the time there are always alternatives.  You use cursors when they make sense to use.  You use collections when they make sense.

The only time you do not have an option is when you issue a select.  A cursor is built for you behind the scenes.

For example: select sysdate from dual;

The results presented to you are pulled from a cursor that you really cannot control.
0
 
pms_reddy1Author Commented:
REF CURSORS will always be recognized by the client program, whatever the language ..
 whereas you may have bad surprises with collections support.
 If "collection" is implemented, hence you will need lot of bandwidth between server & client to pass all the data from server to client.
 using collections could be much worse in terms of memory consumption.
0
 
pms_reddy1Author Commented:
I have done some more research on this topic and able to find some important points  which I did not find the given answer.
0
 
pms_reddy1Author Commented:
Hi slightwv,
I got some information from some sites and thought of providing for the users knowledge.
Infact it is not something for you,
Atleast I am reading some links and gathering some information,where as you are not reading the questions properly and pasting the related links for the sake of points&grades.

anyway Thanks for your time & efforts,
0
 
pms_reddy1Author Commented:
Please close the request.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.