오라클의 시퀀스와 비슷한 기능

 

자동증가하는 값을 만들기위해 indentity를 사용한다..

 

사용법 create table test(t_no int identity(1,1)); (시작할 숫자값, 증가할 숫자값);

   

Identity를 설정해준 테이블에 인서트한 경우 인서트한 identity값을 불러오기 위해서는 보통 다시한번 select를 해주게 된다.
하지만 insert를 하면서 바로
select @@Identity라고 하면 insert한 row의 identity값을 불러올 수 있다.

eg)
INSERT INTO jobs (job_desc,min_lvl,max_lvl)
VALUES ('Accountant',12,125)
SELECT @@IDENTITY AS 'Identity'

 

 

 

/***identity 값을 초기활 때 사용 && 값을 임의로 다른값으로 바꾸고 싶을때 사용***/

 

DBCC CHECKIDENT([table_name], RESEED, 0)

 

/***identity***/

identity 를 로우 넘같이 사용하는 방법... 지저분하다..

 

SELECT IDENTITY(int,1,1) AS RowNum, stor_id, qty
 INTO #Temp1
FROM pubs..sales ORDER BY qty

SELECT * FROM #Temp1 --WHERE RowNum = 5
DROP TABLE #Temp1

 

 

마지막 IDENTITY 값 얻기 SQL2000

 //테이블 행 몇개지 알아 내기 .

* Scope_Identity() gets the last inserted identity in a scope (function,
procedure, trigger, or batch)

* IDENT_CURRENT('table_name') gets you the last identity for a table in any
scope or connection.
//마지막 값 얻기                                                                          보드 이름
insert into board( title , w_content ) values('testest',IDENT_CURRENT('board'));
 
 

+ Recent posts