■テーブルの構成
CREATE TABLE [TABLE1] (
[pid] [int] IDENTITY (1, 1) NOT NULL ,
[tex1] [char] (10) COLLATE Japanese_CI_AS NULL ,
[tex2] [char] (10) COLLATE Japanese_CI_AS NULL ,
CONSTRAINT [PK_TABLE1] PRIMARY KEY CLUSTERED
(
[pid]
) ON [PRIMARY]
)
GO
[pid] [int] IDENTITY (1, 1) NOT NULL ,
[tex1] [char] (10) COLLATE Japanese_CI_AS NULL ,
[tex2] [char] (10) COLLATE Japanese_CI_AS NULL ,
CONSTRAINT [PK_TABLE1] PRIMARY KEY CLUSTERED
(
[pid]
) ON [PRIMARY]
)
GO
■実行したSQL
begin transaction
insert into table1(tex1, tex2)values('1','2')
print '■table1'
SELECT '@@IDENTITY' , @@IDENTITY
SELECT 'SCOPE_IDENTITY' , SCOPE_IDENTITY()
SELECT 'IDENT_CURRENT' , IDENT_CURRENT('table1')
insert into table2(tex1, tex2)values('1','2')
print '■table2'
SELECT '@@IDENTITY' , @@IDENTITY
SELECT 'SCOPE_IDENTITY' , SCOPE_IDENTITY()
SELECT 'IDENT_CURRENT' , IDENT_CURRENT('table1')
rollback
insert into table1(tex1, tex2)values('1','2')
print '■table1'
SELECT '@@IDENTITY' , @@IDENTITY
SELECT 'SCOPE_IDENTITY' , SCOPE_IDENTITY()
SELECT 'IDENT_CURRENT' , IDENT_CURRENT('table1')
insert into table2(tex1, tex2)values('1','2')
print '■table2'
SELECT '@@IDENTITY' , @@IDENTITY
SELECT 'SCOPE_IDENTITY' , SCOPE_IDENTITY()
SELECT 'IDENT_CURRENT' , IDENT_CURRENT('table1')
rollback
■実行結果
■table1
@@IDENTITY 9
SCOPE_IDENTITY 9
IDENT_CURRENT 9
■table2
@@IDENTITY 1
SCOPE_IDENTITY 1
IDENT_CURRENT 9
@@IDENTITY 9
SCOPE_IDENTITY 9
IDENT_CURRENT 9
■table2
@@IDENTITY 1
SCOPE_IDENTITY 1
IDENT_CURRENT 9
※table1は8件、table2は0件の状態です。
(注意)GDD Blog: [.NET]INSERT時の自動採番される値を取得する(SQLSERVER)(その3)で問題点を記載しています。こちらも参照してください。
0 件のコメント:
コメントを投稿