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.

DeleteWithOutput for Azure SQL (SqlServer.2017) generates unsupported query

See original GitHub issue

Hi guys, I found a little problem (which I can bypass).

I have quite trivial code:

using var scope     = _serviceProvider.CreateScope();
var       dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>().CreateLinqToDbConnection();
var deleted = await dbContext.GetTable<CachedResult>()
                             .Where(r => r.Expired < DateTime.UtcNow)
                             .DeleteWithOutputAsync(stoppingToken);

It seems that it generates SQL query, which is not supported in Azure SQL:

--  SqlServer.2017 (asynchronously)
DECLARE @UtcNow DateTime2
SET     @UtcNow = '2022-02-28T15:42:39.9584090'

DELETE [r]
OUTPUT
        *
FROM
        [mytable].[CachedResults] [r]
WHERE
        [r].[Expired] < @UtcNow
16:55:19.818 759860 ERR #  7  LinqToDB.Data.DataConnection:() :: Error
Exception: Microsoft.Data.SqlClient.SqlException
Message  : Incorrect syntax near '*'.
   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__188_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at LinqToDB.Data.DataConnection.ExecuteReaderAsync(CommandBehavior commandBehavior, CancellationToken cancellationToken)

I think a star (*) needs to be prefixed with deleted. to have a query like:

DELETE [r]
OUTPUT
        deleted.*
FROM
        [mytable].[CachedResults] [r]
WHERE
        [r].[Expired] < @UtcNow

https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=azuresqldb-current

I will be much appreciated to any comments.

Environment details

linq2db version: 3.6.0 Database Server: Azure SQL (SqlServer.2017) Database Provider: linq2db.EntityFrameworkCore v6.6.1 Operating system: WIN10 .NET: v6.0

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
MaceWinducommented, Feb 28, 2022

Thanks, we are planning to release reworked OUTPUT support in next release. Will make sure it works with new version

0reactions
MaceWinducommented, May 26, 2022

Closing for now. Will reopen if you still have an issue with it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DELETE (Transact-SQL) - SQL Server
Returns deleted rows, or expressions based on them, as part of the DELETE operation. The OUTPUT clause is not supported in any DML...
Read more >
Delete statement with OUTPUT clause in azure sql
The problem was due to column order being switched for two columns. Thanks for confirming that the OUTPUT clause was working as expected...
Read more >
The OUTPUT Clause for INSERT and DELETE Statements
In this article, I will provide a set of examples to show case the use of OUTPUT clause for INSERT and DELETE statements....
Read more >
Python serverless functions to query Azure SQL DB
This article will show how to get data from Azure SQL in the Serverless Python app.
Read more >
Delete records from single Azure DB table that has no linking
Change your query from delete from [databasename].[dbo].[tblImages] where companyid = 123. to delete from [dbo].
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