identity(標(biāo)識(shí))列,也有很多人稱之為自增列,在sql server 2000中,標(biāo)識(shí)列通過identity來定義,下面是與獲取最后插入記錄的標(biāo)識(shí)值有關(guān)的函數(shù)的一個(gè)示例說明
    sql server 中,可以使用 scope_identity()、 @@identity 、 ident_current() 來取得最后插入記錄的值值,它們的區(qū)別在于:
scope_identity() 返回插入到同一作用域中的 identity 列內(nèi)的最后一個(gè) identity 值。一個(gè)作用域就是一個(gè)模塊——存儲(chǔ)過程、觸發(fā)器、函數(shù)或批處理。因此,如果兩個(gè)語句處于同一個(gè)存儲(chǔ)過程、函數(shù)或批處理中,則它們位于相同的作用域中。
@@identity       返回在當(dāng)前會(huì)話的所有表中生成的最后一個(gè)標(biāo)識(shí)值
ident_current()  返回為任何會(huì)話和任何作用域中的指定表最后生成的標(biāo)識(shí)值
下面以一個(gè)示例來說明它們的區(qū)別 
-- a) 示例代碼 
-- ===========================================
-- 創(chuàng)建測試表
-- ===========================================
use tempdb
go
create table t1(id int identity,col int)
insert t1 select 1
union all select 2
create table t2(id int identity,col int)
go
create trigger tr_insert_t2 on t2
for insert
as
    insert t1 select 3
go 
-- ===========================================
-- 測試三個(gè)函數(shù)..1
-- ===========================================
insert t2 values(1)
select [scope_identity()]=scope_identity(),
    [@@identity][email protected]@identity,
    [ident_current() for t1]=ident_current(n't1'),
    [ident_current() for t2]=ident_current(n't2')
/*--結(jié)果
scope_identity()   @@identity   ident_current() for t1     ident_current() for t2                   
------------------ ------------ -------------------------- -----------------------
1                  3            3                          1
(所影響的行數(shù)為 1 行)
--*/
go
-- ===========================================
-- 測試三個(gè)函數(shù)..2
-- ===========================================
insert t1 values(10)
select [scope_identity()]=scope_identity(),
    [@@identity][email protected]@identity,
    [ident_current() for t1]=ident_current(n't1'),
    [ident_current() for t2]=ident_current(n't2')
/*--結(jié)果
scope_identity()   @@identity   ident_current() for t1     ident_current() for t2                   
------------------ ------------ -------------------------- -----------------------
4                  4            4                          1
(所影響的行數(shù)為 1 行)
--*/
go
-- ===========================================
-- 測試三個(gè)函數(shù)..3
-- ** 開啟一個(gè)新連接,執(zhí)行下面的代碼 **
-- ===========================================
select [scope_identity()]=scope_identity(),
    [@@identity][email protected]@identity,
    [ident_current() for t1]=ident_current(n't1'),
    [ident_current() for t2]=ident_current(n't2')
/*--結(jié)果
scope_identity()   @@identity   ident_current() for t1     ident_current() for t2                   
------------------ ------------ -------------------------- -----------------------
null               null         4                         &n 
--===========================================
-- 刪除測試環(huán)境
-- ===========================================
drop table t1,t2 
-- b) 代碼結(jié)果說明 
從上面的代碼可以看到:
ident_current()  始終返回指定表最后插入的標(biāo)識(shí)值
@@identity       返回當(dāng)前會(huì)話的標(biāo)識(shí)值,無論是否在同一個(gè)作用域,在測試1、2中,可以看到它返回的是觸發(fā)器中插入記錄的標(biāo)識(shí)值,而在測試3中,因?yàn)楫?dāng)前會(huì)話無插入記錄,所以返回null
scope_identity() 返回當(dāng)前會(huì)話同一作用域的標(biāo)識(shí)值,所以在測試1、2中,它返回的值不受觸發(fā)器的影響,而在測試3中,因?yàn)楫?dāng)前會(huì)話無插入記錄,所以返回null
 
新聞熱點(diǎn)
疑難解答
圖片精選