Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Queries returning multiple result sets

Tags:

I have a MSSQL database and am running the following query:

select * from projects; select * from user 

The above query returns two result sets at once, and I cannot fire both queries separately. How can I handle both the result set at once in a Java class?

like image 999
Vishu Singhvi Avatar asked Mar 14 '12 05:03

Vishu Singhvi


People also ask

How do I return multiple result sets with Sqlcommand?

Create a Stored Procedure that has multiple selects, and fill the DataSet . The returned dataset will have a DataTable in it's Tables array for each select in the stored procedure. Show activity on this post. Show activity on this post.

Is it possible to return multiple result sets in Execute SQL task?

Multiple Result Sets with the Execute SQL Task. In reality, data retrieval queries are typically configured to return exactly one result set. If two different result sets are needed, two separate queries are built, each with its own source-to-target flow of data.

Which method used to execute SQL statement which returns multiple result?

In addition, when running a statement that returns more than one result set, you can use the execute method of the SQLServerStatement class, because it will return a boolean value that indicates if the value returned is a result set or an update count.

What can be used to return multiple result sets?

In order to get multiple result sets working we need to drop to the ObjectContext API by using the IObjectContextAdapter interface. Once we have an ObjectContext then we can use the Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal.


2 Answers

Correct code to process multiple ResultSets returned by a JDBC statement:

PreparedStatement stmt = ...; boolean isResultSet = stmt.execute();  int count = 0; while(true) {     if(isResultSet) {         rs = stmt.getResultSet();         while(rs.next()) {             processEachRow(rs);         }          rs.close();     } else {         if(stmt.getUpdateCount() == -1) {             break;         }          log.info("Result {} is just a count: {}", count, stmt.getUpdateCount());     }      count ++;     isResultSet = stmt.getMoreResults(); } 

Important bits:

  • getMoreResults() and execute() return false to indicate that the result of the statement is just a number and not a ResultSet.
  • You need to check stmt.getUpdateCount() == -1 to know if there are more results.
  • Make sure you either close the result sets or use stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT)
like image 51
Aaron Digulla Avatar answered Sep 19 '22 09:09

Aaron Digulla


You can use Statement.execute(), getResultSet();

PreparedStatement stmt = ... prepare your statement result boolean hasResults = stmt.execute(); while (hasResults) {     ResultSet rs = stmt.getResultSet();     ... your code parsing the results ...     hasResults = stmt.getMoreResults(); } 
like image 29
Honza Avatar answered Sep 22 '22 09:09

Honza