34,593
社区成员
发帖
与我相关
我的任务
分享
Use test
Go
If object_id('c_test') Is Not Null
Drop Proc c_test
If object_id('test') Is Not Null
Drop Table test
Go
Create Table test(id int Identity(1,1) Primary Key, x int)
Go
Create Proc c_test
(
@id int Output,
@x int
)
As
Insert Into Test (x) Values (@x)
Set @id=Scope_identity()
Go
Declare @newid int
Exec c_test @newid Output,525
Select @newid As [刚Insert的ID]
/*
刚Insert的ID
-----------
1
*/
Use test
Go
If object_id('c_test') Is Not Null
Drop Proc c_test
If object_id('test') Is Not Null
Drop Table test
Go
Create Table test(id int Identity(1,1) Primary Key, x int)
Go
Create Proc c_test
(
@id int Output,
@x int
)
As
Insert Into Test (x) Values (@x)
Set @id=Scope_identity()
Go
Declare @newid int
Exec c_test @newid Output,525
Select @newid As [刚Insert的ID]
/*
刚Insert的ID
-----------
1
*/
select ident_current('tb')+1
select max(id)+1
select max(id)+1
insert tablename(col1,col2) select @@identity
--@@IDENTITY和SCOPE_IDENTITY和IDENT_CURRENT的区别
@@IDENTITY--是得到当前会话的所有范围的最后插入的IDENTITY值
SCOPE_IDENTITY--是得到当前会话的当前范围的最后插入的IDENTITY值
IDENT_CURRENT--是得到指定表的最后插入的IDENTITY值,与会话、范围无关。