简体   繁体   English

如何调用仅使用SELECT的存储过程

[英]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 前面的代码是一堆MS SQL存储的proc,本质上,它只返回1或0。但是最后要做的是

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. 而且我无法访问调用它的C#代码,因此我不确定它是如何工作的。 Its result is 0 or 1, but it also manages to return those 5 fields from the select. 其结果为0或1,但它也设法从选择中返回那5个字段。

There aren't any OUT parameters. 没有任何OUT参数。 How is one supposed to get at the results of a SELECT in a stored procedure (in Java/JDBC/JPA/any method)? 在存储过程中(在Java / JDBC / JPA /任何方法中)如何获得SELECT的结果?

I've tried a few things with both the jTDS and SQLServer drivers. 我已经尝试使用jTDS和SQLServer驱动程序进行一些操作。

The syntax that you use is insert into . . . exec 您使用的语法是insert into . . . exec 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. insert 文档包含更多示例和信息。

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. Erland Sommerskog关于在SQL Server中的存储过程之间传递数据的博客非常出色,解释了几种方法的优点和缺点。

try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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