반응형

/********************************************************************************************
-- Title : [ORA10g] 채번테이블을 이용한 Oracle 채번
-- Reference : scarlett.tistory.com
-- Key word : 채번
********************************************************************************************/

일련번호를 만들기 위하여

흔히 max값을 구하고 거기에 1을 더하여 일련번호를 만든다.

select max(seq) + 1 into :seq from testtable

insert testtable(seq) values ( :seq)

동시에 여러개의 작업이 돌면 일련번호 중복이 발생할 수 있다.


transaction을 걸경우

begin tran

select max(seq) + 1 into :seq from testtable

insert testtable (seq) values ( :seq)

commit

max를 구하는 select 문에 lock이 걸리지 않기 때문에 역시 일련번호 중복 발생함



 

create table numseq

(

gubn char(2) primary key,

seq decimal

)

insert numseq values('01', 1) -- 채번테이블 초기화


 

begin tran

update numseq

set seq = seq + 1

where gubn = '01'

select seq into :seq from numseq where gubn = '01'

commit

insert testable(seq) values (:seq)


 

채번테이블에 update lock이 발생하고 transaction 이 있기때문에

중복이 발생하지 않는다.

채번테이블에는 1개의 row만 있기때문에 max를 구하는 것보다

채번속도가 빠르다.

 

 

반응형

+ Recent posts