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