我在存储过程中定义了一个临时表变量,其中一列使用了@@identity
declare @temp_table table(f_id identity(1,1))在存储过程中有向其他表插入数据的操作,此表中有一个字增长的主键。
insert into tableA .......执行完成后我发现tableA表中的主键值并不是从1开始增长,而是从某个数开始增长,某个数的值恰巧是插入表变量的行数是不是这个@@identity这个变量在一个存储过程中是共用的,我如何解决这个问题。
谢谢大家
declare @temp_table table(f_id identity(1,1))在存储过程中有向其他表插入数据的操作,此表中有一个字增长的主键。
insert into tableA .......执行完成后我发现tableA表中的主键值并不是从1开始增长,而是从某个数开始增长,某个数的值恰巧是插入表变量的行数是不是这个@@identity这个变量在一个存储过程中是共用的,我如何解决这个问题。
谢谢大家
我如何向临时表插入值后,把@@identity清零
create table #ta
(
id int identity(1,1),
col1 int
)
create table #tb
(
id int identity(1,1),
col1 int
)
set identity_insert #ta off
set identity_insert #tb on
--(2)当设置identity_insert选项值为 ON的时候,可以插入id.
create table #te
(
id int identity(1,1),
col1 varchar(10)
)
insert into #te values ('aa')
insert into #te values ('bb')
select * from #te
set identity_insert #te on
insert into #te(id,col1) values (10,'ff')
set identity_insert #te off
insert into #te values ('hh') 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/02/06/3866850.aspx
IDENT_CURRENT 不受作用域和会话的限制,而受限于指定的表。IDENT_CURRENT 返回为任何会话和作用域中的特定表所生成的值
SCOPE_IDENTITY 和 @@IDENTITY 返回在当前会话中的任何表内所生成的最后一个标识值。但是,SCOPE_IDENTITY 只返回插入到当前作用域中的值;@@IDENTITY 不受限于特定的作用域。