DBA

SQL에서 Unique Code 만들기 본문

[2] Database/MS-SQL

SQL에서 Unique Code 만들기

코볼 2011. 3. 3. 11:26
728x90
반응형
SMALL

ident_current('table_name')
지정된 테이블 또는 뷰에 대해 생성된 마지막 ID값을 반환한다.
생성된 마지막 ID값은 임의의 세션및 범위에 대한 값일 수 있음.

table_name : ID 값을 반환할 테이블 이름, varchar 이며 기본값  없음.

반환형식 : numeric(38, 0)



declare
 @currentDateTime datetime
 , @expireDateTime datetime
 , @codeZero varchar(38)

set @currentDateTime = getdate()
set @expireDateTime = '9999-12-31 23:23:59.997'
set @codeZero = '00000000000000000000000000000000000000'


insert into table_namevalues ('PRJ' + right(@codeZero + ltrim(ident_current('projectHistories')), 38)
         , 'CAS' + right(@codeZero + ltrim(ident_current('projectHistories')), 38)
         , 1, 0, 0, @currentDateTime, @currentDateTime, @expireDateTime)
select * from table_name


1 PRJ00000000000000000000000000000000000001 CAS00000000000000000000000000000000000001 1 0 0
2 PRJ00000000000000000000000000000000000002 CAS00000000000000000000000000000000000002 1 0 0
3 PRJ00000000000000000000000000000000000003 CAS00000000000000000000000000000000000003 1 0 0
4 PRJ00000000000000000000000000000000000004 CAS00000000000000000000000000000000000004 1 0 0
5 PRJ00000000000000000000000000000000000005 CAS00000000000000000000000000000000000005 1 0 0
6 PRJ00000000000000000000000000000000000006 CAS00000000000000000000000000000000000006 1 0 0
7 PRJ00000000000000000000000000000000000007 CAS00000000000000000000000000000000000007 1 0 0
8 PRJ00000000000000000000000000000000000008 CAS00000000000000000000000000000000000008 1 0 0
9 PRJ00000000000000000000000000000000000009 CAS00000000000000000000000000000000000009 1 0 0
10 PRJ00000000000000000000000000000000000010 CAS00000000000000000000000000000000000010 1 0 0

728x90
반응형
LIST
Comments