I have the following Merge statement I’m working with:
MERGE dbo.UnitOfMeasure AS target
USING (
SELECT * FROM dbo.UnitOfMeasure
) AS src
ON
target.UOMId = src.UOMId
WHEN NOT MATCHED THEN
INSERT
(
UOMId,
UOM
)
VALUES
(
src.UOMId,
src.UOM
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE <-- Do something other than delete here
WHEN MATCHED THEN
UPDATE SET
target.UOM = src.UOM;
Instead of actually deleting the records, I’d like to run a stored procedure for each record that was going to be deleted. In essence, I can’t simply delete these records. Instead, I have stored procedure that handles the deleting for me. I just need to pass in the unique ID (UOMId) to the stored proc.
Anyone know of way to do this using the Merge statement?
Thanks!
If I read the documentation correctly, it appears that the only options available to you from with the WHEN NOT MATCHED BY SOURCE clause are UPDATE and DELETE: http://technet.microsoft.com/en-us/library/bb510625.aspx.
Lame! Executing a procedure would be endlessly useful here! Even an INSERT would be sufficient for most scenarios…
Nevertheless, in your case, I would process these records in two steps. 1: Use MERGE with an UPDATE statement to flag the fields to be deleted. 2: Call your deletion code on the flagged fields.
Good luck!