I have a merge statement that runs every day as new data is dumped into my table every day, here is a sample of my code:
MERGE SQL_Backup as Target
USING Temp as Source
ON target.code = source.code
WHEN MATCHED THEN update SET
target.saledate = source.saledate,
target.branchcode = source.branchcode
WHEN NOT MATCHED BY TARGET
THEN insert (
code, saledate
)
values (
source.code, source.saledate
)
when not matched by source
then delete;
however, that deletes my entire table, I am only wanting it to delete rows that were added today, any previous rows shouldnt be touched.
Here is an image of what both the source and target table look like:
I have a column labeled 'saledate' which contains the date of the data dumped in, and I have a unique column labeled 'code' which i used for the merge to detect duplicates.
Was wondering if theres a when not matched by source for saledate = today then delete or something?