简体   繁体   English

存储过程仅返回多行中的一行

[英]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

My Problem 我的问题

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,结果也不会改变。

Something might help 可能有帮助

when I call that stored procedure from my Visual studio , I got 17 results like this: 当我从Visual Studio调用该存储过程时,我得到了17个结果,如下所示:

在此处输入图片说明

Any help will be appreciated 任何帮助将不胜感激

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.

相关问题 MyBatis:Sybase存储过程返回零行 - MyBatis: Sybase stored procedure returns zero rows 我在Java代码中调用了存储过程,该存储过程仅返回一行而不是15行 - I called the stored procedure in my java code its returning only one row instead of 15 rows 如何从存储过程返回多行? - How to return multiple rows from the stored procedure? 我用过cursor_loop:在sql存储过程中循环,它不提取全部15行,仅提取一行 - I used cursor_loop: loop in sql stored procedure its not fetching all 15 rows it fetch one row only 在哈希图中搜索将返回所有存储的值,而不仅仅是搜索到的值 - Searching in a hash map returns all values stored, not just the one that is searched for 从Spring调用带有多行返回的Oracle存储过程 - Calling an Oracle Stored Procedure with multiple row return from Spring 在返回多行的 jdbc 模板中调用存储过程 - calling stored proc in jdbc template which returns multiple rows MySQL查询将多行转换为一行 - MySQL query to convert multiple rows into one row 如何调用仅使用SELECT的存储过程 - How to call Stored Procedure that just uses a SELECT Java存储过程在Oracle数据库中不返回任何内容 - Java stored procedure returns nothing in Oracle Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM