Created
July 20, 2015 14:12
-
-
Save kowill/ee308aa420244992054f to your computer and use it in GitHub Desktop.
MERGEのテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table MergeTest( | |
Id int unique not null, | |
Memo nvarchar(200) | |
); | |
insert into MergeTest values(1, 'test1') | |
insert into MergeTest values(2, 'test2') | |
insert into MergeTest values(3, 'test3') | |
insert into MergeTest values(4, 'test4') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare @id int | |
declare @memo nvarchar(200) | |
set @id = 4; | |
set @memo = 'change'; | |
merge MergeTest as target | |
using (select @id, @memo) as source(Id, Memo) | |
on (target.Id = source.Id) | |
when matched then | |
update set target.Memo = source.Memo | |
when not matched then | |
insert (Id, Memo) values(source.Id, source.Memo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
merge MergeTest as target | |
using (values(4, 'change2'), (5, 'insert')) as source(Id, Memo) | |
on (target.Id = source.Id) | |
when matched then | |
update set target.Memo = source.Memo | |
when not matched then | |
insert (Id, Memo) values(source.Id, source.Memo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment