简体   繁体   中英

How to call Stored Procedure that just uses a SELECT

I'm replacing some code by previous developers, and need to compare my results with the old results.

The previous code was a bunch of MS SQL stored procs, and essentially, it just returns 1 or 0. But the last thing it does is

select key, cd,  descr, sum(quantity) quantity, sum(price) price
from ##output

and I don't have access to the C# code calling it, so I am not sure how this works. Its result is 0 or 1, but it also manages to return those 5 fields from the select.

There aren't any OUT parameters. How is one supposed to get at the results of a SELECT in a stored procedure (in Java/JDBC/JPA/any method)?

I've tried a few things with both the jTDS and SQLServer drivers.

The syntax that you use is insert into . . . exec insert into . . . exec insert into . . . exec . Something like:

insert into t(col1, . . . coln)
    exec sp_stored_procedure ;

This will store the results from the stored procedure into the table. The table needs to be predefined, which means that you need to know the columns in advance and will get an error if they are not correct. The documentation for insert contains more examples and information.

I think that using such a procedure is a bad way to return result sets from stored procedures. In many cases, such stored procedures can be replaced with user defined functions that return tables.

Erland Sommerskog has an excellent blog on passing data between stored procedures in SQL Server, explaining the benefits and downsides to several methods.

try this

PreparedStatement ps = conn.prepareStatement("SELECT * FROM proc1(?)");
ps.set...
ResultSet rs = ps.executeQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM