I've a Pipeline in Synapse which syncs data from a source SQL database to a destination SQL database. As soon as the copy data activity is done it inserts a record (with a stored procedure) in a log table.
This table is used to sync data incremental. As soon as I start my PipeLine again it will compare the date in this log table with the DateChanged date of the source table so that it knows which records to sync.
At the end of the PipeLine run I would like to delete the record with the oldest TransferTime. So that I keep my table clean and small. But I only want to do this when there are more than 1 record for the table. Just to prevent that my whole pipeline fails because it can't find a last TransferTime, so it doesn't know which records are changed synce last run.
In the example below I want to delete the oldest records of Table A and Table B which have the TransferTime 2023-03-28 10:00.
| TableName | TransferTime |
| ------------ | ------------ |
| Table A | 2023-03-28 10:00 |
| Table A | 2023-03-28 10:15 |
| Table B | 2023-03-28 10:00 |
| Table B | 2023-03-28 10:15 |
| Table C | 2023-03-28 10:00 |
2条答案
按热度按时间wmvff8tz1#
You can use
EXISTS
to check rows before deletion:This query deletes row if exists row with the same
TableName
but biggerTransferTime
.hgtggwj02#
Exists provides one canonical way to do this. Given that you are using SQL Server, you could also use a deletable CTE here: