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.

SQLException : Incorrect syntax near the keyword 'UNION'

See original GitHub issue

Linq query with Union and OrderBy does not translate correctly.

Simple:

_context.Subjects.Where(s => !(s is Group))
                  .OrderBy(o => o.Created) 
                   .Take(10)
                   .Union(_context.Subjects.Where(s => s is Group)
                                            .OrderBy(o => o.Created)
                                            .Take(10))
                    .ToList();

This will be translate to =>

Select 
Where
OrderBy  -Error ( select must be in subquery)
UNION 
Select
Where
Order by -Error ( select must be in subquery)

Incorrect syntax near the keyword ‘UNION’

SELECT TOP(@__p_0) [s].[SubjectId], [s].[Address], [s].[Changed], [s].[Created]
FROM [Subjects] AS [s]
WHERE ([s].[Discriminator] IN (N'PhysicalPerson', N'Company', N'PrivateCompany', N'Group') AND (CASE
    WHEN @__ef_filter__p_0 = CAST(1 AS bit) THEN CAST(1 AS bit)
    ELSE CASE
        WHEN (CAST(1 AS bit) = CAST(0 AS bit)) AND ([s].[IsDeleted] <> CAST(1 AS bit)) THEN CAST(1 AS bit)
        ELSE CAST(0 AS bit)
    END
END = CAST(1 AS bit))) AND ([s].[Discriminator] <> N'Group')
ORDER BY [s].[Created]
UNION
SELECT TOP(@__p_0) [s0].[SubjectId], [s0].[Address], [s0].[Changed], [s0].[Created]
FROM [Subjects] AS [s0]
WHERE ([s0].[Discriminator] IN (N'PhysicalPerson', N'Company', N'PrivateCompany', N'Group') AND (CASE
    WHEN @__ef_filter__p_0 = CAST(1 AS bit) THEN CAST(1 AS bit)
    ELSE CASE
        WHEN (CAST(1 AS bit) = CAST(0 AS bit)) AND ([s0].[IsDeleted] <> CAST(1 AS bit)) THEN CAST(1 AS bit)
        ELSE CAST(0 AS bit)
    END
END = CAST(1 AS bit))) AND ([s0].[Discriminator] = N'Group')
ORDER BY [s0].[Created]

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET Core 3.0) Operating system: IDE: (e.g. Visual Studio 2019 16.3)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
smitpatelcommented, Oct 14, 2019
SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
ORDER BY [c].[ContactName]
UNION
SELECT TOP(1) [c0].[CustomerID], [c0].[Address], [c0].[City], [c0].[CompanyName], [c0].[ContactName], [c0].[ContactTitle], [c0].[Country], [c0].[Fax], [c0].[Phone], [c0].[PostalCode], [c0].[Region]
FROM [Customers] AS [c0]
ORDER BY [c0].[ContactName]

Above is invalid SQL but when you push down the whole thing it’s valid. :trollface:

SELECT *
FROM (
	SELECT TOP(1) [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
	FROM [Customers] AS [c]
	ORDER BY [c].[ContactName]
	UNION
	SELECT TOP(1) [c0].[CustomerID], [c0].[Address], [c0].[City], [c0].[CompanyName], [c0].[ContactName], [c0].[ContactTitle], [c0].[Country], [c0].[Fax], [c0].[Phone], [c0].[PostalCode], [c0].[Region]
	FROM [Customers] AS [c0]
	ORDER BY [c0].[ContactName]
) as t
1reaction
rojicommented, Oct 14, 2019

Apologies, I mistested the above - in SQL Server ORDER BY is of course OK in subqueries as long as TOP is present as well. So it’s indeed possible to translate this by pushing down to a subquery in the SQL Server and Sqlite case (again, in the PostgreSQL case an actual subquery isn’t necessary, only parentheses).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incorrect syntax for Union All query - sql
But I am getting this error: Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'union' ...
Read more >
Incorrect syntax near the keyword 'UNION' · Issue #18362 · ...
Linq query with Union and OrderBy does not translate correctly. ... SQLException : Incorrect syntax near the keyword 'UNION' #18362.
Read more >
[Solved] Incorrect syntax near union
Since you have only one SELECT statement, remove the UNION clause from the end of you SQL. ... If that's the case then...
Read more >
Incorrect syntax near the keyword 'UNION'. ????
Hi, Why does this query give this error? SELECT TOP 1 tblSMS.*, tblCampaigns.Mail FROM tblSMS LEFT JOIN tblCampaigns ON tblSms.Campaign = tblCampaigns.
Read more >
System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.'
I am trying to make a simple app that gets your IP address and stores it in a sql database 1 But I...
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