How to retrieve old values in OUTPUT clause with SQL MERGE statement

sql-server

Solution

Can you not just used the `deleted` memory-resident table. e.g:

IF OBJECT_ID(N'tempdb..#T', 'U') IS NOT NULL
    DROP TABLE #T;

CREATE TABLE #T (Name VARCHAR(5), Description VARCHAR(20));
INSERT #T (Name, Description)
VALUES ('a', 'desca'), ('b', 'delete');

MERGE #T AS t
USING (VALUES ('a', 'newdesca'), ('c', 'insert')) AS m (Name, Description)
    ON t.Name = m.Name
WHEN MATCHED THEN 
    UPDATE SET Description = m.Description
WHEN NOT MATCHED BY TARGET THEN 
    INSERT (Name, Description)
    VALUES (m.Name, m.Description)
WHEN NOT MATCHED BY SOURCE THEN
    DELETE
OUTPUT $Action, inserted.*, deleted.*;

IF OBJECT_ID(N'tempdb..#T', 'U') IS NOT NULL
    DROP TABLE #T;

The output of this would be:

$Action | Name  | Description | Name | Description
--------+-------+-------------+------+--------------
INSERT  |   c   |   insert    | NULL | NULL
UPDATE  |   a   |  newdesca   | a    | desca
DELETE  | NULL  |    NULL     | b    | delete

Problem

I'm using MERGE statement to update a product table containing `(Name="a", Description="desca")`. My source table contains `(Name="a", Description="newdesca")` and I merge on the Name field. In my Output clause, I would like to get back the field BEFORE the update -> `Description = "desca"`. I couldn't find a way to do that, I'm always getting back the new value `("newdesca")`. Why?

Original source