2012-01-10 69 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

相關問題