NHibernate - Returning an output parameter from a mapped Stored Procedure
I have a Stored Procedure that I have mapped via NHibernate. The Sproc returns both a resultset, and an output paramter, basically a count of the total records followed by the resultset itself. Something like this:
CREATE PROCEDURE [dbo].[mySproc]
(
@StartRecord INT = 1,
@EndRecord INT = 10,
@TotalRecords INT OUTPUT
)
AS
BEGIN
SET NOCOUNT ON
// Do a count
SELECT @TotalRecords = COUNT(DISTINCT x.Id)
FROM Blah ...snip
// Perform a query with paging
SELECT
x.Id,
x.Column1
FROM ...some really complex query that uses 开发者_Go百科paging
END
I'm implementing paging via a Sproc and thus need to get the total number of records. The query is quite complex with joins across several tables, potentially quite slow, and thus needs the care, fine tuning and optimisation from writing T-SQL directly.
My problem is that I can't get at the OUTPUT parameter in the Sproc (@TotalRecords). I have mapped this Sproc like so:
<class name="MyLibrary.SomeClass, MyLibrary">
<id name="Id" type="Int32" />
<property name="Column1" type="String" length="50" not-null="false" />
</class>
<sql-query name="mySproc">
<return class="MyLibrary.SomeClass, MyLibrary">
<return-property name="Id" column="Id"/>
<return-property name="Column1" column="Column1"/>
</return>
EXEC mySproc
@StartRecord = :startRecord,
@EndRecord = :endRecord,
@TotalRecords = 0
</sql-query>
And I can get at the resultset like so:
IList<SomeClass> records = Session.GetNamedQuery("mySproc")
.SetParameter("startRecord", 1, NHibernateUtil.Int32)
.SetParameter("endRecord", 20, NHibernateUtil.Int32)
.List<SomeClass>();
But how do I get the @TotalRecords output parameter?
It seems it can't be done directly :
- http://osdir.com/ml/nhusers/2010-01/msg00794.html
- http://www.richter-web.info/Wordpress/?p=132
精彩评论