Can we write our query inside the merge syntax...
Ex:
Merge table as T
Using Table as S
on (T.ID=S.ID)
WHEN NOT MATCHED BY TARGET
THEN
{insert query}
-- Here, after inserting records i want to delete them with my CTE as below
[
with CTE_dup as
(
{query}
)
delete from CTE_dup where where ID in (1,2,3)
]
WHEN MATCHED
THEN UPDATE
WHEN NOT MATCHED BY SOURCE
THEN DELETE