nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

NH-3594 - MSSql2012Dialect generates invalid order by clause for paging distinct query

Open nhibernate-bot opened this issue 7 years ago • 2 comments

Gerke Geurts created an issue:

The SQL Server 2012 dialect generates invalid SQL when paging or limits are used on SELECT DISTINCT queries. The NHibernate.Test.NHSpecificTest.NH2214.Fixture.ShouldWorkUsingDistinctAndLimits() results in the following exception:

NHibernate.Exceptions.GenericADOException : could not execute query
----> System.Data.SqlClient.SqlException : ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Generated SQL:

select distinct person0_.Id as Id5061_, person0_.FirstName as FirstName5061_ 
from Person person0_
ORDER BY CURRENT_TIMESTAMP OFFSET 0 ROWS FETCH FIRST @p0 ROWS ONLY

Gerke Geurts added a comment — :

Fix proposed in https://github.com/nhibernate/nhibernate-core/pull/251


Oskar Berggren added a comment — :

Merged in 8531dafd9e42fc4de9b00dc27934038520e28112.


Oskar Berggren added a comment — :

Merge reverted due to many new test failures for e.g. sql server 2008.

nhibernate-bot avatar Oct 12 '17 22:10 nhibernate-bot

Hi! Is there any workaround?

xumix avatar May 14 '19 15:05 xumix

Is there any workaround?

Adding explicitly order by clause to query:

select distinct p from Person p order by p.id desc

bahusoid avatar May 16 '19 09:05 bahusoid