검색을 해보아도 찾을수 가 없어서 이렇게 질문 드립니다.
MS-SQL 에서 mysql 의 LAST_INSERT_ID() 와 같은 역활을 하는 함수가 있나요?
방금 인서트 한 레코드의 자동증가 id 값은 어떻게 가져올까요?
부탁드립니다.
SELECT @@IDENTITY
하심 됩니다.
친절한 답변에 감사드립니다.
select @@identity
select ident_current('테이블명')
평상시에는 똑같습니다.
만약에...
set identity_insert 테이블명 on
을 한 후
강제로 identity를 입력시킨다면..
select @@identity는 임의로 아니 강제로 입력된 값의 최대값을
불러오지만..
select ident_current('테이블명')은
시스템상에서 입력받은 identity의 최대값을 보여주게 됩니다.
create table a( id int identity, name char(10))
insert a(name) values('c')insert a(name) values('b')
insert a(name) values('d')insert a(name) values('e')
select @@identity, ident_current('a')
----------
4 | 4
둘다 4, 4 로 똑같은 결과가 나옴..
하지만,
set identity_insert a on -- 강제로 identity값을 입력시키고자 할때...
insert a(id,name) values(1,'d')
insert a(id,name) values(2,'g')
select * from a
id name
1 c
2 b
3 d
4 e
1 d
2 g
2 | 4
앞의 @@identity 값의 2는 사용자가
강제로 identity번호를 준 최대값 표시
뒤의 ident_current('a') 의 4는 시스템상에서 주어진
identity번호의 최대값 표시