I am going to use MERGE to insert or update a table depending upon ehether it's exist or not. This is my query,
declare @t table
(
id int,
name varchar(10)
)
insert into @t values(1,'a')
MERGE INTO @t t1
USING (SELECT id FROM @t WHERE ID = 2) t2 ON (t1.id = t2.id)
WHEN MATCHED THEN
UPDATE SET name = 'd', id = 3
WHEN NOT MATCHED THEN
INSERT (id, name)
VALUES (2, 'b');
select * from @t;
The result is,
id name
1 a
I think it should be,
id name
1 a
2 b