본문 바로가기

DB/MS-SQL

[SQL]IDENT_CURRENT 와 SCOPE_IDEENTITY


drop tabel t
create table t
(
id int identity(1, 1),
val char(1)
)


declare @i int
set @i = 1
while @i <= 3
begin
insert t
(val)
values
('a')
set @i = @i + 1
end

select * from t
select [scope_identity] = scope_identity()
select [ident_current] = ident_current('t')
select [max] = max(id) from t

결과

id             val
1               a
2               a
3               a
(3개 행 적용됨)

scope_identity
3

(1개 행 적용됨)

ident_current
3

(1개 행 적용됨)

max
3

(1개 행 적용됨)

새로운 윈도우(ctrl + n)를 3개 값 입력 실행

declare @i int
set @i = 1
while @i <= 3
begin
insert t
(val)
values
('a')
set @i = @i + 1
end

현재 윈도우로 돌아와서 아래를 실행

select * from t
select [scope_identity] = scope_identity()
select [ident_current] = ident_current('t')
select [max] = max(id) from t

결과

id              val
1                a
2                a
3                a
4                a
5                a
6                a

(6개 행 적용됨)

scope_identity
3

(1개 행 적용됨)

ident_current
6

(1개 행 적용됨)

max
6

(1개 행 적용됨)

출처 : http://blog.naver.com/ccw3435

'DB > MS-SQL' 카테고리의 다른 글

[MS-SQL] SQL 실행 순서  (0) 2013.09.30
[MS-SQL] 쿼리문 작성 순서  (0) 2013.09.30
[MS-SQL] ms-sql 단축키  (0) 2011.12.01
[T-SQL] while문 예  (0) 2011.09.29
[MS-SQL] 문법  (0) 2011.09.28