분노의 챔질
[MSSQL] Merge사용 본문
declare @Test table
(
EmployeeID nvarchar(10),
EmployeeName nvarchar(30)
)
insert @Test
select '1', '김'
union all
select '209001', '홍'
select * from @Test
declare @TestInfo table
(
RowStatus nvarchar(10),
EmployeeID nvarchar(10),
EmployeeName nvarchar(30)
)
insert @TestInfo
select 'Modified', '209001', '홍길동'
union all
select 'Added', '209002', '갑돌이'
union all
select 'Deleted', '1', '김'
merge @Test as E
using @TestInfo as Info on
E.EmployeeID = Info.EmployeeID
when matched and RowStatus = 'Modified' then
update
set E.EmployeeName = Info.EmployeeName
when matched and RowStatus = 'Deleted' then
delete
when not matched and RowStatus = 'Added' then
insert (EmployeeID, EmployeeName)
values (Info.EmployeeID, Info.EmployeeName)
;
select * from @Test
'Programming > SQL' 카테고리의 다른 글
MS SQL 데이터베이스 사이즈 줄이기 (0) | 2010.11.23 |
---|---|
MS-SQL 백업복구 쿼리 (0) | 2010.07.14 |
[MSSQL] WITH common_table_expression(Transact-SQL) (0) | 2010.03.31 |
[MSSQL] 간단한 DATE함수 (0) | 2010.03.23 |
[SQL] 공통코드 사용 (0) | 2010.03.17 |