sql server 2000 @@IDENTITY和SCOPE_IDENTITY和IDENT_CURRENT的区别
2006-08-23 17:31
519 查看
@@IDENTITY和SCOPE_IDENTITY和IDENT_CURRENT的区别
1,@@IDENTITY是得到当前会话的所有范围的最后插入的IDENTITY值
2,SCOPE_IDENTITY是得到当前会话的当前范围的最后插入的IDENTITY值
3,IDENT_CURRENT是得到指定表的最后插入的IDENTITY值,与会话、范围无关。
因为在插入和得到IDENTITY值之间可能会有其它的事情发生,但是你只想得到我刚才插入的IDENTITTY值,只有使用SCOPE_IDENTITY函数才行。
以下是测试SQL
(1)
USE pubs
DROP TABLE t6
DROP TABLE t7
GO
CREATE TABLE t6(id int IDENTITY)
CREATE TABLE t7(id int IDENTITY(100,1))
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT
AS
BEGIN
INSERT t7 DEFAULT VALUES
END
GO
--end of trigger definition
SELECT * FROM t6
--id is empty.
SELECT * FROM t7
--id is empty.
--Do the following in Session 1
INSERT t6 DEFAULT VALUES
declare @i int
set @i = 0
while @i < 20000
begin
set @i = @i + 1
print @i
end
SELECT @@IDENTITY as "@@IDENTITY0"
/*Returns the value 100, which was inserted by the trigger.*/
SELECT SCOPE_IDENTITY() 'SCOPE_IDENTITY()0'
/* Returns the value 1, which was inserted by the
INSERT stmt 2 statements before this query.*/
SELECT IDENT_CURRENT('t7') 'IDENT_CURRENT(t7)0'
/* Returns value inserted into t7, i.e. in the trigger.*/
SELECT IDENT_CURRENT('t6') 'IDENT_CURRENT(t6)0'
/* Returns value inserted into t6, which was the INSERT statement 4 stmts before this query.*/
-- Do the following in Session 2
SELECT @@IDENTITY "@@IDENTITY1"
/* Returns NULL since there has been no INSERT action
so far in this session.*/
SELECT SCOPE_IDENTITY() 'SCOPE_IDENTITY()1'
/* Returns NULL since there has been no INSERT action
so far in this scope in this session.*/
SELECT IDENT_CURRENT('t7') 'IDENT_CURRENT(t7)1'
/* Returns the last value inserted into t7.*/
当在执行while @i < 20000
begin
set @i = @i + 1
print @i
end这个地方的时候,就可以用
INSERT t6 DEFAULT VALUES
INSERT t7 DEFAULT VALUES
select * from t6
select * from t7来进行操作数据库
等到全部执行完了,你就会发现只有SCOPE_IDENTITY列永远是1
1,@@IDENTITY是得到当前会话的所有范围的最后插入的IDENTITY值
2,SCOPE_IDENTITY是得到当前会话的当前范围的最后插入的IDENTITY值
3,IDENT_CURRENT是得到指定表的最后插入的IDENTITY值,与会话、范围无关。
因为在插入和得到IDENTITY值之间可能会有其它的事情发生,但是你只想得到我刚才插入的IDENTITTY值,只有使用SCOPE_IDENTITY函数才行。
以下是测试SQL
(1)
USE pubs
DROP TABLE t6
DROP TABLE t7
GO
CREATE TABLE t6(id int IDENTITY)
CREATE TABLE t7(id int IDENTITY(100,1))
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT
AS
BEGIN
INSERT t7 DEFAULT VALUES
END
GO
--end of trigger definition
SELECT * FROM t6
--id is empty.
SELECT * FROM t7
--id is empty.
--Do the following in Session 1
INSERT t6 DEFAULT VALUES
declare @i int
set @i = 0
while @i < 20000
begin
set @i = @i + 1
print @i
end
SELECT @@IDENTITY as "@@IDENTITY0"
/*Returns the value 100, which was inserted by the trigger.*/
SELECT SCOPE_IDENTITY() 'SCOPE_IDENTITY()0'
/* Returns the value 1, which was inserted by the
INSERT stmt 2 statements before this query.*/
SELECT IDENT_CURRENT('t7') 'IDENT_CURRENT(t7)0'
/* Returns value inserted into t7, i.e. in the trigger.*/
SELECT IDENT_CURRENT('t6') 'IDENT_CURRENT(t6)0'
/* Returns value inserted into t6, which was the INSERT statement 4 stmts before this query.*/
-- Do the following in Session 2
SELECT @@IDENTITY "@@IDENTITY1"
/* Returns NULL since there has been no INSERT action
so far in this session.*/
SELECT SCOPE_IDENTITY() 'SCOPE_IDENTITY()1'
/* Returns NULL since there has been no INSERT action
so far in this scope in this session.*/
SELECT IDENT_CURRENT('t7') 'IDENT_CURRENT(t7)1'
/* Returns the last value inserted into t7.*/
当在执行while @i < 20000
begin
set @i = @i + 1
print @i
end这个地方的时候,就可以用
INSERT t6 DEFAULT VALUES
INSERT t7 DEFAULT VALUES
select * from t6
select * from t7来进行操作数据库
等到全部执行完了,你就会发现只有SCOPE_IDENTITY列永远是1
相关文章推荐
- 获取sql server中自增量之scope_identity(),@@Identity,IDENT_CURRENT的区别
- Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别
- SQL Server 2000中,SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY
- sql server 2000 @@identity、Scope_identity()、ident_current的区别
- SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的区别
- scope_identity(), @@IDENTITY, IDENT_CURRENT()区别
- <转>SQL Server返回最后一个标识值的三个函数:IDENT_CURRENT、@@IDENTITY、SCOPE_IDENTITY
- ◎◎identity ,ident_current() ,scope_identity的区别
- @@IDENTITY、IDENT_CURRENT、SCOPE_IDENTITY的区别
- sql 中获取最后生成的标识值 IDENT_CURRENT ,@@IDENTITY ,SCOPE_IDENTITY 的用法和区别
- 测试@@IDENTITY,SCOPE_IDENTITY,IDENT_CURRENT的区别
- SQL Server -- @@identity , SCOPE_IDENTITY,IDENT_CURRENT 三者的异同
- SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY 的区别
- ◎◎identity ,ident_current() ,scope_identity的区别
- @@identity、scope_identity()、IDENT_CURRENT('tablename')函数的区别
- SQL Server返回最后一个标识值的三个函数:IDENT_CURRENT、@@IDENTITY、SCOPE_IDENTITY
- 关于如何使用SqlServer中@@Identity、@@IDENT_CURRENT、@@SCOPE_Identity获取自动生成列的值的区别(精简版)
- IDENT_CURRENT、@@IDENTITY 和 SCOPE_IDENTITY 的区别
- SQL 获取 IDENTITY 三种方法 SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的区别
- SQL查询 [SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的区别(比较)]