user2893780
user2893780

Reputation: 121

SQL Merge and output in the same table

I'm merging 2 tables and I want that if the cell is update the field would be marked as "updated" my code:

 MERGE [ITWORKS].[dbo].[Testine2] te
    USING [ITWORKS].[dbo].[Testinus] bo
    ON te.itemid = bo.itemid
    AND te.itemname <> bo.itemname
    WHEN MATCHED THEN
      UPDATE
      SET te.itemname = bo.itemname
     OUTPUT
        $action
       into  [ITWORKS].[dbo].[Testine2] (busena);
    SELECT * FROM [ITWORKS].[dbo].[Testine2];

Result I get:

Itemid  Itemname Busena 
100001  TEST      NULL 
NULL    Null      UPADTE

The result I want:

Itemid  Itemname Busena
100001  TEST      UPDATE

Upvotes: 0

Views: 208

Answers (1)

Mikael Eriksson
Mikael Eriksson

Reputation: 138990

I want that if the cell is update the field would be marked as "updated"

There is no reason to use output. Just set the column value in the update.

MERGE [ITWORKS].[dbo].[Testine2] te
    USING [ITWORKS].[dbo].[Testinus] bo
    ON te.itemid = bo.itemid
    AND te.itemname <> bo.itemname
    WHEN MATCHED THEN
      UPDATE
      SET te.itemname = bo.itemname,
          te.Busena = 'UPDATE';

SELECT * FROM [ITWORKS].[dbo].[Testine2];

Upvotes: 2

Related Questions