SQL Server 2005, 2008의 새로운 기능 – OUTPUT

 

안녕하세요! SQL Server Frontier의 산아입니다.

SQL Server 2005, 2008 데이터베이스 엔진의 향상된 프로그래밍 기능에는 Transact-SQL에 대한 다양한 개선 사항 및 추가 기능이 있습니다.

 

이번에는 SQL Server 2005, 2008 Transact-SQL의 새로운 기능인 OUTPUT에 대한 내용을 다루어보려 합니다.

 

어플리케이션의 요청에 의해 INSERT, UPDATE, DELETE 등의 작업 후 해당 정보를 보내주어야 하는 작업은 흔히 경험해보셨을 텐데요, 매번 각 작업마다 해당 결과를 SELECT하는 등의 별도 비용을 발생시켜 처리하는 나름 손가락을 귀찮게(?) 만드는 작업이기도 합니다.

 

이런 약간의 귀찮음을 OUTPUT 기능을 통해 조금 더 편하게 사용할 수 있게 되었습니다. ^^

 

보다 자세한 내용은 BOL을 참고하세요~

 

http://msdn.microsoft.com/ko-kr/library/ms177564.aspx

 

use tempdb;

go

 

drop table dbo.t1;

set nocount on;

 

begin try

       create table dbo.t1

       (

               col1 int not null identity(1, 1) primary key

             , col2 int

       );

end try

begin catch

       truncate table dbo.t1;

       dbcc checkident ('t1', reseed, 1);

end catch

go

 

-- 2005에서는 insert절의 value 구문에서 , 사용한 여러 데이터 입력을 없습니다.

-- 기능은 2008 추가된 기능이며 최대 1000 까지 입력할 있습니다.^^

-- http://www.sqler.com/?mid=bColumn&page=2&document_srl=130321

 

insert dbo.t1 (col2)

output inserted.col1, inserted.col2

values (11111), (22222), (333333), (1242142);

go

col1        col2

----------- -----------

1           11111

2           22222

3           333333

4           1242142

 

update dbo.t1

       set col2 = 112

output inserted.col1

, deleted.col2 as old_data

, inserted.col2 as new_data

where col1 = 3;

go

col1        old_data    new_data

----------- ----------- -----------

3           333333      112

 

delete dbo.t1

output deleted.col1 as delete_col1

, deleted.col2 as delete_col2

where col1 = 4;

go

delete_col1 delete_col2

----------- -----------

4           1242142

 

 





profile

Happy SQLER~!

NEXON NDOORS DBA. 

SQLER.com Sysop / SQLTAG Vice-master / Microsoft SQL Server Frontier Group.

E-Mail: alucard@naver.com / Mobile: +82 10-5665-8554

http://www.sqler.comhttp://www.sqltag.org / http://alucardkang.tistory.com