Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC and the fly likes Get ResultSet by executing anonymous pl/sql block. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Get ResultSet by executing anonymous pl/sql block." Watch "Get ResultSet by executing anonymous pl/sql block." New topic
Author

Get ResultSet by executing anonymous pl/sql block.

Rajukpo kumar
Greenhorn

Joined: Dec 31, 2010
Posts: 9
Hi all,

We are using Oracle with PL/SQL.
I am using an anonymous blocks to get the data. I need to run about four different select queries and be able to navigate through it. It will have to have loops within loops. I did a similar thing in Java and i just assigned all the results to a Vector (or I suppose an array could also have worked), iterate through it with a loop (iterate through a different resultset within this loop) and then do some checks.

QUESTION: How do I navigate through the results (in SQL) after I ran the query? I read somewhere about cursors, but is this the only way? I see that all don't recommend it. If I should use a cursor, how does it work. What is the syntax, etc?

Any help..?

Thanks,
Raj
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

...I did a similar thing in Java and i just assigned all the results to a Vector....

How you've done this? Can you show us the code?
Note that the Vector is synchronized unlike other collection implementations.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

If you want to program in PL/SQL, you should read the documentation. If you still have problems, come back here to ask specific questions. For handling cursors, see chapter 6 - Static SQL. (I've given link to most recent documentation, if you have a different version of Oracle, find corresponding documentation.)

Cursors are the standard way to handle data in PL/SQL, they cannot be avoided, and are used even by JDBC to retrieve data from the database. Thus the recommendation to not use them does not make sense. (The only way to avoid cursors is to manipulate data using SQL - see the next point).

However, you should reconsider whether the thing you want to program in PL/SQL is not doable using plain SQL. Oracle can efficiently execute even very complicated SQL statements with lots of joined tables, and it will always be much more performant than any PL/SQL construct. Even merging some of the four selects together and eg. processing two instead of four would be benefical. "Loops of loops" type of operation sounds like an operation that would be very good candidate to be performed in SQL. Complicated updates of joined tables are also possible.

If you do it in PL/SQL and are not on 11g, definitely use bulk collect - a kind of batching. In 11g, this optimization si done automatically behind the scenes.

And finally, if you do need PL/SQL, consider using stored procedure (or better - package). Anonymous blocks get compiled anew with every execution, while stored procedures are compiled once (upon creation) and then reused.

Edit: It should be possible to return an array from PL/SQL using collections. You might also create a function that returns a ref cursor. I'd suggest you to consider using pure SQL first, then you won't have to research into returning collections from PL/SQL block. It should definitely be doable, but I don't have any particular experience with it.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1617
    
  13

See Martin's reply above. Cursors (which return result sets) are how you use SQL in procedural contexts such as Java or PL/SQL, although as Martin says, you may be able to do everything in one big SQL statement which could make things easier.

As for looping within loops, think about what you are trying to do in terms of sets of data, not individual rows. Many 3GL developers have problems getting out of the procedural row-by-row mentality, but SQL is designed to perform bulk manipulations on sets of data (rows/colums) and provides powerful tools to do this e.g. joins, WHERE filters, sub-SELECTs, combinations of UNION/INTERSECT/MINUS, top-N queries etc. If you can re-define your problem in these terms (which is very often possible) you will find that SQL is a far more powerful and efficient way to solve it.

Once again, if you're planning to do any serious work with an RDBMS, you really need to learn how to use these tools properly.


No more Blub for me, thank you, Vicar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get ResultSet by executing anonymous pl/sql block.
 
Similar Threads
Searching a ResultSet
Use PL/SQL Loop and variable
is using ArrayList.iterator() is faster than looping ArrayList in for loop?
how to find out how many records returned in recordSet
calling a PL/SQL procedure from a Java application