MS-SQL Server

SQL2008의 새로운 기능 - Composable DML

송혁 - HyokSong 2008. 1. 22. 00:04

SQL Server 2005부터 OUTPUT절이 추가 되어 UPDATE, INSERT, DELETE 작업에 대한 대상 행을 테이블 변수로 저장하여 활용할 수 있었습니다.

하지만 대상 데이터를 다른 테이블에 저장하기 위해서는 테이블 변수로 저장 후 다시 저장해야 하는 번거로움이 있었습니다.


이러한 문제에 대해서 몇 가지 제한 사항이 존재 하지만 SQL Server 2008은 OUTPUT절을 포함하는 UPDATE, INSERT, DELETE, MERGE의 OUTPUT결과를 하나의 구문으로 다른 테이블에

저장할 수 있는 기능을 제공하고 있습니다.


해당 기능은 CTP5부터 지원되며, 보다 자세한 내용은 SQL Server 2008 BOL에서 INSERT 부분을 참고하세요.

use tempdb

go

--drop table tblSource

--drop table tblTarget

create table tblSource(col1 int)

create table tblTarget(col1 int)


--delete tblSource

--delete tblTarget

insert into tblSource values(1),(2),(3),(4),(5),(6)


--INSERT

insert into tblTarget(col1)

select col1

from (insert into tblSource output inserted.col1 values(10),(11),(12) ) as d;


--UPDATE

insert into tblTarget(col1)

select col1

from (update tblSource set col1 = col1 + 1 output deleted.col1) as d;


--DELETE

insert into tblTarget(col1)

select col1 from (delete tblSource output deleted.col1 where col1%2 = 0)X

송 혁, SQL Server MVP

sqler.pe.kr

nexondbteam.tistory.com