SQLException : Incorrect syntax near the keyword 'UNION'
See original GitHub issueLinq 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:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Above is invalid SQL but when you push down the whole thing it’s valid. :trollface:
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).