NHibernate意外的ORDER BY语句

时间:2009-03-31 15:24:29

标签: sql nhibernate sql-order-by

我正在使用NHibernate 2.0,当我提交请求返回前2条记录的请求时,我在SQL中获得了许多ORDER BY子句。当我取出Max结果时,查询看起来很好(没有ORDER BY语句)。为什么NHibernate会在寻找记录子集时自动添加它?提前致谢

请参阅下面的SQL语句:

意外的ORDER BY

exec sp_executesql 
N'SELECT TOP 2 Person1_36_0_, LastReco2_36_0_, SSN3_36_0_,
 FirstName4_36_0_, LastName5_36_0_, MiddleIn6_36_0_, Title7_36_0_, Suffix8_36_0_, 
DateOfBi9_36_0_, IsDeceased10_36_0_, Decease11_36_0_, Contact12_36_0_, MailHol13_36_0_, 
MailHol14_36_0_, MailHol15_36_0_, Preferr16_36_0_, CreatedBy17_36_0_, Created18_36_0_, 
ModifiedBy19_36_0_, Modifie20_36_0_ 
FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_0__) as row, 
query.Person1_36_0_, query.LastReco2_36_0_, query.SSN3_36_0_, query.FirstName4_36_0_, 
query.LastName5_36_0_, query.MiddleIn6_36_0_, query.Title7_36_0_, query.Suffix8_36_0_, 
query.DateOfBi9_36_0_, query.IsDeceased10_36_0_, query.Decease11_36_0_, 
query.Contact12_36_0_, query.MailHol13_36_0_, query.MailHol14_36_0_, query.MailHol15_36_0_, 
query.Preferr16_36_0_, query.CreatedBy17_36_0_, query.Created18_36_0_, 
query.ModifiedBy19_36_0_, query.Modifie20_36_0_, query.__hibernate_sort_expr_0__
FROM 
(SELECT this_.Person_id as Person1_36_0_, this_.[LastRecordVersion] as LastReco2_36_0_, 
this_.[SSN] as SSN3_36_0_, this_.[FirstName] as FirstName4_36_0_, this_.[LastName] as 
LastName5_36_0_, this_.[MiddleInitial] as MiddleIn6_36_0_, this_.[Title] as Title7_36_0_, 
this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased] 
as IsDeceased10_36_0_, this_.[DeceasedDate] as Decease11_36_0_, this_.[ContactMethod_id] as 
Contact12_36_0_, this_.[MailHoldReason_id] as MailHol13_36_0_, this_.[MailHoldStartDate] as 
MailHol14_36_0_, this_.[MailHoldEndDate] as MailHol15_36_0_, this_.[PreferredName] as 
Preferr16_36_0_, this_.[CreatedBy] as CreatedBy17_36_0_, this_.[CreatedDate] as 
Created18_36_0_, this_.[ModifiedBy] as ModifiedBy19_36_0_, this_.[ModifiedDate] as 
Modifie20_36_0_, CURRENT_TIMESTAMP as __hibernate_sort_expr_0__ 
FROM MC_Person this_ 
WHERE this_.[SSN] = @p0) query) page 
WHERE **page.row > 0 ORDER BY __hibernate_sort_expr_0__**',
N'@p0 nvarchar(9)',@p0=N'123456789'

更正SQL(未获得前2条记录)

exec sp_executesql 
N'SELECT this_.Person_id as Person1_36_0_, this_.[LastRecordVersion] as LastReco2_36_0_, 
this_.[SSN] as SSN3_36_0_, this_.[FirstName] as FirstName4_36_0_, this_.[LastName] as 
LastName5_36_0_, this_.[MiddleInitial] as MiddleIn6_36_0_, this_.[Title] as Title7_36_0_, 
this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased] 
as IsDeceased10_36_0_, this_.[DeceasedDate] as Decease11_36_0_, this_.[ContactMethod_id] as 
Contact12_36_0_, this_.[MailHoldReason_id] as MailHol13_36_0_, this_.[MailHoldStartDate] as 
MailHol14_36_0_, this_.[MailHoldEndDate] as MailHol15_36_0_, this_.[PreferredName] as 
Preferr16_36_0_, this_.[CreatedBy] as CreatedBy17_36_0_, this_.[CreatedDate] as 
Created18_36_0_, this_.[ModifiedBy] as ModifiedBy19_36_0_, this_.[ModifiedDate] as 
Modifie20_36_0_
FROM MC_Person this_ 
WHERE this_.[SSN] = @p0',
N'@p0 nvarchar(9)',@p0=N'123456789'

2 个答案:

答案 0 :(得分:1)

这是实现分页的方式。因此,只有前n个元素没有特殊情况,因为例如Oracle不支持这种结构。

所以任何设置了分页限制的东西都是这样做的。

你得到了正确的结果吗?

答案 1 :(得分:0)

如果没有排序,最高选择没有多大意义,因为订购可能会因数据库表的维护而改变。

难道你不知道如何定义你想要的前两种结果?因此顺序由?

相关问题