2012-01-10 67 views
0

在执行daoMethod()我得到以下异常:值java.sql.SQLException:过程或函数...希望...的参数,但未提供

值java.sql.SQLException:过程或函数' Get_Books'期望参数'@totalRowsReturned',它没有提供。

为什么?我确定将@totalRowsReturned定义为OUTPUT。我不明白为什么我需要提供@totalRowsReturned - 它是一个输出参数,而不是输入。

DAO类:

public class BookDao { 

    @Autowired 
    DataSource dataSource; 

    public void daoMethod() { 

     Integer programIdLocal = null; 

     Map<String, Object> parameters = new HashMap<String, Object>(); 
     parameters.put("bookId", 1); 

     MyStoredProcedure storedProcedure = new MyStoredProcedure(dataSource);  

     //Exception!!!! 
     Map<String, Object> results = storedProcedure.execute(parameters); 

    } 

    private class MyStoredProcedure extends StoredProcedure { 

     private static final String SQL = "dbo.Get_Books"; 

     public MyStoredProcedure(DataSource dataSource) { 
      setDataSource(dataSource); 
      setFunction(true); 
      setSql(SQL); 

      declareParameter(new SqlReturnResultSet("rs", new BookMapper())); 

      declareParameter(new SqlOutParameter("totalRowsReturned", Types.INTEGER)); 

      declareParameter(new SqlParameter("bookId", Types.INTEGER)); 

      setFunction(true); 

      compile(); 
     } 

    } 
} 

存储过程:

CREATE PROCEDURE [dbo].[Get_Books] 

    @bookId int, 
    @totalRowsReturned int OUTPUT 

AS 

BEGIN 

    SET NOCOUNT ON; 
    DECLARE @SelectQuery NVARCHAR(2000) 

    DECLARE @first_id int 
    DECLARE @totalRows int 

    SET @SelectQuery = 'FROM books b WHERE b.book_id >= @bookId' 

    Set @SelectQuery = 'SELECT @first_id = b.book_id , @totalRows=Count(*) OVER() ' + @SelectQuery + ' ORDER BY b.book_id' 
    Execute sp_Executesql @SelectQuery, N'@first_id int, @bookId int, @totalRows int OUTPUT', @first_id, @bookId, @[email protected] OUTPUT 

END 

回答

2

有一个在the Javadoc for StoredProcedure#declareParameter()一个重要的警告,必须在它们在存储过程中声明的顺序声明的参数,大概是因为the underlying CallableStatement class存在相同的限制。这意味着你应该在@totalRowsReturned之前声明@bookId。另外,我不是所有关于JdbcTemplate的知识,但是,based on this example,我不认为你需要声明一个结果集参数。

+0

谢谢 - 我改变了顺序,这个异常消失了(我也必须改成'setFunction(false);' - 因为你的程序不是一个函数,问题是:我现在得到异常:* * java.sql.SQLException:必须声明标量变量“@totalRows”。**任何想法为什么? – rapt 2012-01-11 03:48:20

+0

这不是问题的解决方案 – divine 2016-06-01 07:26:30

相关问题