output 의 사용

북스온라인 보면 잘 나오고 어딘가 질문이 있어서 하나 만들어 둡니다.

OUTPUT 절의 활용

if object_id ('Seat') is not null
drop table Seat
go
if object_id ('reservedSeat') is not null
drop table reservedSeat
go


create table Seat
( seatNo int
, seatRow char(1)
, isReserved bit
)
go

set nocount on
begin tran

insert into Seat values (1,'a',0)
insert into Seat values (2,'a',0)
insert into Seat values (3,'a',0)
insert into Seat values (4,'a',0)
insert into Seat values (5,'a',0)
commit tran

go

create table reservedSeat
(reservedNo int
,seatNo int
,seatRow char(1)
)
go

UPDATE SEAT
 SET ISRESERVED = 1
 OUTPUT INSERTED.SEATNO
  , INSERTED.SEATNO
  , INSERTED.SEATROW
 INTO RESERVEDSEAT
WHERE
 SEATNO IN (1,2,3)
 AND ISRESERVED <> 1
GO

SELECT @@ROWCOUNT
GO

-- 반드시 적용된 개수를 확인해서 COMMIT 과  ROLLBACK 처리를 해주어야 합니다.

SELECT * FROM SEAT
SELECT * FROM RESERVEDSEAT
왕눈봄이(minsouk1)