[英]stored procedure returns just one row of multiple rows
I will start by giving you my table content 我将从给我我的桌子内容开始
This is my stored procedure: 这是我的存储过程:
ALTER PROCEDURE dbo.getFoodsForOrder
(
@orderID INT,
@ID INT OUTPUT,
@food_restaurantID INT OUTPUT,
@count INT OUTPUT,
@description VARCHAR(200) OUTPUT
)
AS
SET NOCOUNT ON
BEGIN
SELECT [ID],
[food_restaurantID],
[count],
[description]
FROM Order_Food
WHERE orderID = @orderID
END
when I call the stored procedure from JDBC like this 当我像这样从JDBC调用存储过程时
Connection con = Database.getConnection();
CallableStatement callableStatement = null;
try {
callableStatement = con
.prepareCall("{call getFoodsForOrder(?,?,?,?,?)}");
callableStatement.setInt(1, getID());
callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
callableStatement.registerOutParameter(3, java.sql.Types.INTEGER);
callableStatement.registerOutParameter(4, java.sql.Types.INTEGER);
callableStatement.registerOutParameter(5, java.sql.Types.VARCHAR);
System.out.println("ID = " + getID());
boolean haveResult = callableStatement.execute();
while (haveResult) {
System.out.println("here I am");
haveResult = callableStatement.getMoreResults();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (callableStatement != null)
callableStatement.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
it just print here I am
once, just once, even If (like the picture said) I have more than 15 rows are ture. 它只是
here I am
打印一次,一次,即使我有超过15行也是如此(如图所示)。
you problem will ask me if I am sure about the getID()
method, Yes I am sure and even when I replace it with explicit 10 , the result doesn't change. 您的问题将询问我是否确定
getID()
方法,是的,我确定,即使将其替换为显式10,结果也不会改变。
when I call that stored procedure from my Visual studio , I got 17 results like this: 当我从Visual Studio调用该存储过程时,我得到了17个结果,如下所示:
The callableStatement.execute()
indicates if a result set is available. 该
callableStatement.execute()
表示,如果结果集是可用的。
getMoreResults()
only indicates if there are is another ResultSet available, not if the "last" result set has more rows (which is - I think - pretty clear from the JavaDocs). getMoreResults()
仅指示是否存在另一个 ResultSet ,而不指示“最后一个”结果集是否具有更多行(从JavaDocs来看,这很明显)。 getMoreResults()
would return true
if you had more than one select
statement in your procedure. 如果您的过程中有多个
select
语句,则getMoreResults()
将返回true
。
If execute()
indicates a ResultSet is available you need to obtain it using callableStatement.getResultSet()
and then iterate over the rows returned by that. 如果
execute()
表示ResultSet 是可用的,你需要使用获得它callableStatement.getResultSet()
然后遍历通过返回的行。
Something like: 就像是:
boolean haveResult = callableStatement.execute();
if (haveResult) {
ResultSet rs = callableStatement.getResultSet();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} else {
System.out.println("Nothing returned");
}
But as you know it returns a result set you can also call getResultSet()
right away without even bothering about the return value of the execute()
call. 但是,您知道它返回了一个结果集,所以您也可以立即调用
getResultSet()
,而不必担心execute()
调用的返回值。
You need to remove the OUTPUT
variables from your stored procedure and no longer register them in your code. 您需要从存储过程中删除
OUTPUT
变量,并且不再在代码中注册它们。 You then need to use executeQuery()
to get the resultset and iterate over the resultset to get rows. 然后,您需要使用
executeQuery()
获取结果集,并遍历结果集以获取行。
Assuming a table structure of: 假设表结构为:
CREATE TABLE [dbo].[Order_Food](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OrderID] [int] NULL,
[food_restaurantID] [int] NULL,
[count] [int] NULL,
[description] [nvarchar](255) NULL,
CONSTRAINT [PK_Order_Food] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Table content: 表格内容:
INSERT INTO [dbo].[Order_Food]
([OrderID], [food_restaurantID], [count], [description])
VALUES (513, 2, 3, 'Description xyz'), (513, 2, 3, 'Description xyz'),
(132, 1, 2, 'Description abc');
A stored procedure as: 存储过程为:
CREATE PROCEDURE dbo.getFoodsForOrder
(
@orderID INT
)
AS
SET NOCOUNT ON
BEGIN
SELECT [ID],
[food_restaurantID],
[count],
[description]
FROM Order_Food
WHERE orderID = @orderID
END
And query code as: 并查询代码为:
public static void main(String[] args) throws SQLException {
try (
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=Scratchspace;integratedSecurity=true");
CallableStatement callableStatement =
con.prepareCall("{call getFoodsForOrder(?)}");
){
callableStatement.setInt(1, 513);
System.out.println("ID = " + 513);
ResultSet rs = callableStatement.executeQuery();
while (rs.next()) {
System.out.printf("%d %d %d %s%n",
rs.getInt("ID"), rs.getInt("food_restaurantID"),
rs.getInt("count"), rs.getString("description"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
On my computer this produces the output: 在我的计算机上,这将产生输出:
ID = 513
1 2 3 Description xyz
2 2 3 Description xyz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.