国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

SQL SERVER 2000 中的標(biāo)識(shí)值獲取函數(shù)

2024-08-31 00:49:20
字體:
供稿:網(wǎng)友

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


 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 仲巴县| 牡丹江市| 江孜县| 阳城县| 昭苏县| 布尔津县| 兖州市| 平顺县| 措勤县| 精河县| 永定县| 睢宁县| 航空| 鹰潭市| 航空| 乐清市| 文水县| 上犹县| 蒲江县| 墨脱县| 饶平县| 绥阳县| 宕昌县| 弥勒县| 蒙自县| 钦州市| 乐都县| 庆阳市| 亳州市| 呼和浩特市| 柞水县| 罗甸县| 平邑县| 营口市| 洛扎县| 天气| 大新县| 汨罗市| 南充市| 织金县| 北碚区|