question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong generated SQL for SQL Server 2012/2014 (DISTINCT / OFFSET)

See original GitHub issue

I am using this provider:

SqlServerDataProvider(ProviderName.SqlServer2012, SqlServerVersion.v2012);

Which produces the following SQL query (simplified for demo):

exec sp_executesql N'SELECT DISTINCT
    [test].[Id]
FROM
    [dbo].[Test] [test]
ORDER BY
    [test].[Time] DESC,
    [test].[Id] DESC
OFFSET 0 ROWS FETCH NEXT 60 ROWS ONLY'

But it is not valid SQL: “ORDER BY items must appear in the select list if SELECT DISTINCT is specified.”

Linq:

var query = from test in storage.Tests
            orderby test.Time descending, test.Id descending
            select test.Id;

var tests = query.Distinct().Page(page, size).ToList();

Internally Page method uses Skip and Take to get desired page.

When I am using 2008 provider, it generates ROW_NUMBER and all works fine:

exec sp_executesql N'SELECT *
FROM
(
    SELECT
        t.*,
        ROW_NUMBER() OVER
        (
            ORDER BY
                oby DESC,
                oby1 DESC
        ) as rn
    FROM
    (
        SELECT DISTINCT
            [test].[Id],
            [test].[Time] as [oby],
            [test].[Id] as [oby1]
        FROM
            [dbo].[Test] [test]
    ) t
) t1
WHERE
    t1.rn BETWEEN 1 AND 60'

In this specific case (when Id of table is used) I can implement workaround by including Time in query and by wrapping offset query with Id selection. But it is only possible because (Id, Time) will be always unique due to Id. But in common scenario there are no way to order rows and select page if ordering columns are not targets in query.

So it is not good that something can work for SQL Server 2008 and cannot work for 2012/2014 one.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:20 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Feb 28, 2017

@ili, it is not equivalent queries. You’ve missed proper ordering by Time in result query.

0reactions
MaceWinducommented, Jul 25, 2018

Fixed by #1221

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incorrect syntax near OFFSET command - sql
My compatibility level is 130 (SQL Server 2016) but still see the same error. This is an SQL Server DW instance on Azure...
Read more >
Distinct function error in sql server - Microsoft Q&A
Hello All, I have a query and I need to get distinct of a field. Datatype is varchar. But Its throwing below error....
Read more >
Error 15023 User already exists in the current database in ...
In this post i will discuss about Error 15023 User already exists in the current database in SQL Server which can occur between...
Read more >
Whats wrong with DISTINCT in this query?
Hello all. Ive got the following SQL query and i want to return only distinct [ReviewID]'s, however SQL Server is complaining about it....
Read more >
Learn Sql Server Tutorial
Its main purpose is to build and maintain databases. It is used to analyze the data using SQL Server Analysis Services (SSAS). It...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found