【Data Platform】/⤷ SQL, T-SQL

[2k] 테이블 변수 및 임시 테이블의 공유 영역 확인(다른 프로시저에 호출된 프로시저)

디비랑 2008. 9. 7. 00:51

/**********************************************************************************************
-- Title : [2k] 테이블 변수 및 임시 테이블의 공유 영역 확인(다른 프로시저에 호출된 프로시저)
-- Reference : dBRang.com
-- Key word : 테이블변수, 임시테이블
**********************************************************************************************/
use tempdb
go

/*
-- 임시 테이블 중첩 확인
*/
drop proc up_ttt
drop proc up_ttt2

-- 프로시저 1.
create proc up_ttt
as
   set xact_abort on
   create table #ttt (a int not null primary key)
 
begin tran
   insert into #ttt values (1)
   insert into #ttt values (2)
   insert into #ttt values (3)
   insert into #ttt values (4)
   insert into #ttt values (5)
commit
   select * from #ttt
 
   exec up_ttt2
go

-- 프로시저 2.
create proc up_ttt2
as
   set xact_abort off
begin tran
   insert into #ttt values (11)
   insert into #ttt values (12)
   insert into #ttt values (1)  --에러 : 임시 테이블이 호출된 프로시저에서도 유지된다는걸 보여 주고 싶은거야?
   insert into #ttt values (14)
   insert into #ttt values (15)
commit
   select * from #ttt
go

exec up_ttt

/*
-- 테이블 변수 중첩 확인
*/
drop proc up_ttt3
drop proc up_ttt4

-- 프로시저 1.
create proc up_ttt3
as
   set xact_abort on
   declare @ttt table
   (a int not null primary key)
 
begin tran
   insert into @ttt values (1)
   insert into @ttt values (2)
   insert into @ttt values (3)
   insert into @ttt values (4)
   insert into @ttt values (5)
commit
   select * from @ttt
 
   exec up_ttt4
go

-- 프로시저 2.
-- 생성시부터 에러다. 즉, 테이블 변수는 호출된 영역에서 공유되지 못한다.
create proc up_ttt4
as
   set xact_abort off
begin tran
   insert into @ttt values (11)
   insert into @ttt values (12)
   insert into @ttt values (1)
   insert into @ttt values (14)
   insert into @ttt values (15)
commit
   select * from @ttt
go

exec up_ttt4