Active Record or NHibernate generating invalid Sql for paging on SqlQuery

落爺英雄遲暮 提交于 2020-01-03 02:48:05

问题


When applying Paging (using SetFirstResult and SetMaxResults) to an ActiveRecord SqlQuery, with nHibernate 2.1.1 GA and ActiveRecord 2.0.1 the following sql is generated:

SELECT 
    TOP 40 
FROM 
    (, ROW_NUMBER() OVER(ORDER BY account.Name, account.State) as __hibernate_sort_row 
        select  account.Name 
                <rest of query>
    ) as query 
WHERE query.__hibernate_sort_row > 40 
ORDER BY query.__hibernate_sort_row

This errors and moreover doesn't run in sql... whereas it should be

SELECT TOP 40  * 
FROM ( 
    SELECT
        ROW_NUMBER() OVER (ORDER BY account.Name, account.State) as __hibernate_sort_row 
    ,select  account.Name 
                <rest of query>
) as query 
WHERE query.__hibernate_sort_row > 40 
ORDER BY query.__hibernate_sort_row

The odd things are :

  • The query without paging works fine
  • With paging, page 1 works fine (i.e. first result = 0, maxresult = 40)
  • Exactly the same approach works fine for HqlQuery, only SqlQuery affected.

This applies to MS2005Dialect and MS2008Dialect...

Anyone know my stupid issue ?


回答1:


Well I've found this page Possible SQL Server bug which, contra to the title, suggests I'll need to write it in my sql. No problem just could have done without spending half a day trying.

Can anyone confirm this is definitely the case ?




回答2:


I used a work-around for it. Here its link. May be helpful to you.



来源:https://stackoverflow.com/questions/2201125/active-record-or-nhibernate-generating-invalid-sql-for-paging-on-sqlquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!