我使用hibernate访问 sql server 2005但在作分页时排序遇到了问题,似乎必须在前面加上select top 100 percent.....后面才可以加order by ?请问是这样吗?但如果是这样,在hql里应该怎么写啊?

解决方案 »

  1.   

    当不使用select top 100 pecent...的时候,直接order by 会报错:com.microsoft.sqlserver.jdbc.SQLServerException: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.但这样的语句直接在sql的查询分析器里就没问题。
      

  2.   


    declare @t table(id int,name varchar(10))
    insert into @t select 5,'asdf'
    insert into @t select 3,'ww'
    insert into @t select 7,'ss'
    insert into @t select 2,'dd'
    insert into @t select 8,'fff'select * from
    (select * from @t order by id)a
    --以上语句会报错:错误信息很明显。。
    --服务器: 消息 1033,级别 15,状态 1,行 9
    --除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。
    select * from
    (select top 100 percent * from @t order by id)a
    --ok
      

  3.   

    我的sql是这样的:select * from a,b where a.a1>0 and (select count(*) from b where b.b1=a.a1) > 0 order by a.a3问题是如果加上top,hibernate不认啊
      

  4.   


    在子查询里面用了order by 后,一定要加上top