IDENT_CURRENT (Transact-sql)

Belirtilen tablo veya görünüm için oluşturulan son kimlik değeri döndürür. Oluşturulan son kimlik değeri herhangi bir oturumu ve herhangi bir kapsam için olabilir.

Konu bağlantısı simgesi Transact-SQL Sözdizim Kuralları

Sözdizimi

IDENT_CURRENT( 'table_name' )

Bağımsız değişkenler

  • table_name
    Kimlik değerini döndürülen tablonun adıdır. table_nameise varchar, hiçbir varsayılan ile.

Dönüş Türleri

numeric(38,0)

Kural dışı durumlar

Döndürür null bir hata ya da arayan bir nesneyi görüntüleme iznine sahip değil.

De SQL Server, bir kullanıcı yalnızca kullanıcının sahip olduğu ya da üzerinde kullanıcıya izin securables meta verileri görüntüleyebilirsiniz. Bu, IDENT_CURRENT kullanıcı nesnesi üzerinde izni yoksa null döndürebilir gibi meta veri verilirken, yerleşik fonksiyonları olduğunu anlamına gelir. Daha fazla bilgi için, bkz. Meta veri görünürlük yapılandırma.

Açıklamalar

IDENT_CURRENT benzer SQL Server 2000kimlik fonksiyonları @@ IDENTITY ve SCOPE_IDENTITY. Tüm üç fonksiyon son oluşturulan kimlik değerleri döndürür. Ancak, kapsamı ve oturumun son Bu işlevlerden her biri tanımlanan farklıdır:

  • Son kimlik değeri herhangi bir oturumu ve herhangi bir kapsam içinde belirli bir tablo için oluşturulan IDENT_CURRENT döndürür.

  • @@ IDENTITY son kimlik değeri herhangi bir tabloyu geçerli oturumda, tüm kapsamları oluşturulan döndürür.

  • SCOPE_IDENTITY herhangi bir tabloyu geçerli oturumu ve geçerli kapsam içinde oluşturulan son kimlik değeri döndürür.

Zaman (tablo satırları asla yer veya kesildi) IDENT_CURRENT değeri null olduğundan, IDENT_CURRENT işlev tohum değerini verir.

Hata döndüren deyimler ve işlemler bir tablonun geçerli kimliğini değiştirebilir ve kimlik sütunu değerlerinde boşluklar oluşturabilir. Kimlik değerini tabloya eklemeye çalışan işlem yürütülmemiş olsa bile kimlik değeri hiçbir zaman geri alınmaz. Örneğin, bir INSERT deyimi bir IGNORE_DUP_KEY ihlali nedeniyle hata verirse, tablonun geçerli kimlik değeri yine de artırılır.

Sonraki oluşturulan kimlik değerini öngörmek için IDENT_CURRENT kullanırken dikkatli olun. Gerçek oluşturulan değeri diğer oturumlar tarafından yapılan eklemeler nedeniyle IDENT_CURRENT plus IDENT_INCR farklı olabilir.

Örnekler

A.Belirtilen tablo için oluşturulan son kimlik değeri döndüren

Aşağıdaki örnek için oluşturulan son kimlik değeri verir Person.Addressiçinde masa AdventureWorks2012veritabanı.

USE AdventureWorks2012;
GO
SELECT IDENT_CURRENT ('Person.Address') AS Current_Identity;
GO

USE AdventureWorks2012;
GO
SELECT IDENT_CURRENT ('Person.Address') AS Current_Identity;
GO

B.IDENT_CURRENT, @@ IDENTITY ve SCOPE_IDENTITY döndürülen kimlik değerleri karşılaştırma

Aşağıdaki örnek tarafından döndürülen farklı kimlik değerleri gösterir IDENT_CURRENT, @@IDENTITY, ve SCOPE_IDENTITY.

USE AdventureWorks2012;
GO
IF OBJECT_ID(N't6', N'U') IS NOT NULL 
    DROP TABLE t6;
GO
IF OBJECT_ID(N't7', N'U') IS NOT NULL 
    DROP TABLE t7;
GO
CREATE TABLE t6(id int IDENTITY);
CREATE TABLE t7(id int IDENTITY(100,1));
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT 
AS
BEGIN
   INSERT t7 DEFAULT VALUES
END;
GO
--End of trigger definition

SELECT id FROM t6;
--IDs empty.

SELECT id FROM t7;
--ID is empty.

--Do the following in Session 1
INSERT t6 DEFAULT VALUES;
SELECT @@IDENTITY;
/*Returns the value 100. This was inserted by the trigger.*/

SELECT SCOPE_IDENTITY();
/* Returns the value 1. This was inserted by the 
INSERT statement two statements before this query.*/

SELECT IDENT_CURRENT('t7');
/* Returns value inserted into t7, that is in the trigger.*/

SELECT IDENT_CURRENT('t6');
/* Returns value inserted into t6. This was the INSERT statement four statements before this query.*/

-- Do the following in Session 2.
SELECT @@IDENTITY;
/* Returns NULL because there has been no INSERT action 
up to this point in this session.*/

SELECT SCOPE_IDENTITY();
/* Returns NULL because there has been no INSERT action 
up to this point in this scope in this session.*/

SELECT IDENT_CURRENT('t7');
/* Returns the last value inserted into t7.*/

USE AdventureWorks2012;
GO
IF OBJECT_ID(N't6', N'U') IS NOT NULL 
    DROP TABLE t6;
GO
IF OBJECT_ID(N't7', N'U') IS NOT NULL 
    DROP TABLE t7;
GO
CREATE TABLE t6(id int IDENTITY);
CREATE TABLE t7(id int IDENTITY(100,1));
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT 
AS
BEGIN
   INSERT t7 DEFAULT VALUES
END;
GO
--End of trigger definition

SELECT id FROM t6;
--IDs empty.

SELECT id FROM t7;
--ID is empty.

--Do the following in Session 1
INSERT t6 DEFAULT VALUES;
SELECT @@IDENTITY;
/*Returns the value 100. This was inserted by the trigger.*/

SELECT SCOPE_IDENTITY();
/* Returns the value 1. This was inserted by the 
INSERT statement two statements before this query.*/

SELECT IDENT_CURRENT('t7');
/* Returns value inserted into t7, that is in the trigger.*/

SELECT IDENT_CURRENT('t6');
/* Returns value inserted into t6. This was the INSERT statement four statements before this query.*/

-- Do the following in Session 2.
SELECT @@IDENTITY;
/* Returns NULL because there has been no INSERT action 
up to this point in this session.*/

SELECT SCOPE_IDENTITY();
/* Returns NULL because there has been no INSERT action 
up to this point in this scope in this session.*/

SELECT IDENT_CURRENT('t7');
/* Returns the last value inserted into t7.*/

Ayrıca bkz.

Başvuru

@@ IDENTITY (Transact-sql)

SCOPE_IDENTITY (Transact-SQL)

IDENT_INCR (Transact-sql)

IDENT_SEED (Transact-sql)

Ifadeler (Transact-sql)

Sistem işlevler (Transact-sql)