Active Record 或 NHibernate 为 SqlQuery 上的分页生成无效的 Sql

发布于 2024-08-20 06:00:40 字数 1061 浏览 0 评论 0原文

使用 nHibernate 2.1.1 GA 和 ActiveRecord 将分页(使用 SetFirstResultSetMaxResults)应用于 ActiveRecord SqlQuery 时2.0.1 生成以下 sql:

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

这个错误,而且不在 sql 中运行...而它应该是

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

奇怪的是:

  • 没有分页的查询工作正常
  • 使用分页,第 1 页工作正常(即第一个结果 = 0 , maxresult = 40)
  • 完全相同的方法适用于 HqlQuery,只有 SqlQuery 受到影响。

这适用于 MS2005Dialect 和 MS2008Dialect...

有人知道我的愚蠢问题吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2024-08-27 06:00:40

好吧,我找到了这个页面 可能的 SQL Server 错误,与标题相反,这表明我需要将其写入我的 sql 中。不花半天时间尝试就可以解决任何问题。

任何人都可以确认这确实是这样吗?

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 ?

感性 2024-08-27 06:00:40

我为此使用了一个解决方法。这是链接。可能对你有帮助。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文