tech.chakapoko.com
Home / Microsoft SQL Server

[Microsoft SQL Server]CLIでidentity型で採番されたidを取得する

scope_identity() を使います

1> create table users (
2>   id bigint identity primary key,
3>   name varchar(32) not null
4> )
5> GO
1> insert into users (name) values ('John')
2> GO

(1 rows affected)
1> select scope_identity()
2> GO

----------------------------------------
                                       1

(1 rows affected)
1> insert into users (name) values ('John')
2> GO

(1 rows affected)
1> select scope_identity()
2> GO

----------------------------------------
                                       2

(1 rows affected)