34,593
社区成员
发帖
与我相关
我的任务
分享
create table test
(
id int identity(1,1),
a varchar(100)
)
GO
insert test select ('a')
select @@identity
insert test select ('a')
select @@identity
insert test select ('B')
select @@identity
create table test
(
id int identity(1,1),
a varchar(100)
)
GO
insert test select ('a')
print @@identity
insert test select ('a')
print @@identity
insert test select ('B')
print @@identity
或
use test
go
create table test
(
id int identity(1,1),
a varchar(100)
)
GO
insert test select ('a')
print IDENT_CURRENT('test')
--想在此列印出ID号的值!
insert test select ('a')
--想在此列印出ID号的值!
print IDENT_CURRENT('test')
insert test select ('B')
--想在此列印出ID号的值!
print IDENT_CURRENT('test')
create table test
(
id int identity(1,1),
a varchar(100)
)
GO
insert test select ('a')
print @@identity
insert test select ('a')
print @@identity
insert test select ('B')
print @@identity
drop table test
/*
(1 row(s) affected)
1
(1 row(s) affected)
2
(1 row(s) affected)
3
*/
insert test select ('a')
--想在此列印出ID号的值!
select @@identity
insert test select ('a')
--想在此列印出ID号的值!
select @@identity
insert test select ('B')
--想在此列印出ID号的值!
select @@identity
示例
下面的示例向带有标识列的表中插入一行,并用 @@IDENTITY 显示在新行中使用的标识值。
INSERT INTO jobs (job_desc,min_lvl,max_lvl)
VALUES ('Accountant',12,125)
SELECT @@IDENTITY AS 'Identity'