0

Can Oracle Ref Cursors only be used with Procedures and Functions, or can they also be used with PL/SQL issued via an ODP.NET OracleCommand object (contained as a string in the CommandText property)?

2
  • 1
    Procedures and Functions are PL/SQL. Cursors can be used in any Pl/SQL block, i.e. a begin ... end; Commented Jul 2, 2012 at 18:46
  • Yeah, no doubt they are PL/SQL, I'm just trying to find out if Ref Cursors are restricted to use with them, which I believe the 2nd half of your comment answers. I've up-voted your comment, since it won't let me check it as an answer. Commented Jul 2, 2012 at 18:52

1 Answer 1

1

It depends on what you are trying to accomplish.

You can certainly execute an anonymous PL/SQL block from a .Net or a Java application that internally makes use of a REF CURSOR. Since anonymous PL/SQL blocks do not accept or return parameters, however, you cannot return a REF CURSOR that you open in an anonymous PL/SQL block to the calling application.

From an application architecture standpoint, I would strongly advocate that if you need to do processing that requires PL/SQL, that processing ought to be incorporated into a named PL/SQL block (a procedure or a function) that is in a package and stored in the database. Having logic implemented in PL/SQL that is stored outside the database is generally a bad idea.

Sign up to request clarification or add additional context in comments.

4 Comments

I thought the anonymous PL/SQL could return data via output bind variables/parameters in ODP.NET, is that not the case with Ref Cursors? I can appreciate that it is a bad idea to be placed outside the database, unfortunately that decision is out of my hands.
@Paul - I don't believe that is possible with a REF CURSOR. Obviously, I'd welcome someone pointing out a gap in my knowledge.
It would seem it's possible with a stored function (e.g. [stackoverflow.com/questions/6360244/…), but as far as I can determine, you are correct, it is not possible with anonymous PL/SQL.
Good news for anyone who reads this later. In a related question, @Juergen-Hartelt just showed the way to use a Ref Cursor as an output bind variable/parameter with an anonymous PL/SQL command used via C# with ODP.NET (stackoverflow.com/questions/11267746/…).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.