IDENTITY (屬性) (Transact-SQL)

建立資料表中的識別欄位。這個屬性用來搭配 CREATE TABLE 和 ALTER TABLE Transact-SQL 陳述式。

[!附註]

IDENTITY 屬性不同於顯示資料行之資料列識別屬性的 SQL-DMO Identity 屬性。

主題連結圖示Transact-SQL 語法慣例

語法

IDENTITY [ (seed , increment) ]

引數

  • seed
    這是載入資料表的第一個資料列所用的值。

  • increment
    這是加入先前載入的資料列之識別值的累加值。

您必須同時指定種子和遞增,或同時不指定這兩者。如果同時不指定這兩者,預設值便是 (1,1)。

備註

如果識別欄位所在的資料表經常有刪除動作,識別值之間可能會有間距。如果這有問題,請勿使用 IDENTITY 屬性。不過,若要確定沒有間距,或要填補現有的間距,請先評估現有的識別值,再設定 SET IDENTITY_INSERT ON 來明確輸入一個識別值。

如果您重複使用移除的識別值,請利用 B 範例中的相同範例程式碼來尋找下一個可用的識別值。請利用資料表名稱、識別欄位資料類型,以及 (這個資料類型的) 最大允許值的數值 -1 來取代 tablename、column_type 和 MAX(column_type) - 1。

請利用 DBCC CHECKIDENT 來檢查目前的識別值,再比較它和識別欄位中的最大值。

如果發行含識別欄位的資料表來進行複寫,您必須依照使用的複寫類型所適用的方式,來管理識別欄位。如需詳細資訊,請參閱<複寫識別欄位>。

範例

A. 搭配 CREATE TABLE 使用 IDENTITY 屬性

下列範例會利用自動累加的識別碼之 IDENTITY 屬性,來建立一份新的資料表。

USE AdventureWorks
IF OBJECT_ID ('dbo.new_employees', 'U') IS NOT NULL
   DROP TABLE new_employees
GO
CREATE TABLE new_employees
(
 id_num int IDENTITY(1,1),
 fname varchar (20),
 minit char(1),
 lname varchar(30)
)

INSERT new_employees
   (fname, minit, lname)
VALUES
   ('Karin', 'F', 'Josephs')

INSERT new_employees
   (fname, minit, lname)
VALUES
   ('Pirkko', 'O', 'Koskitalo')

B. 利用一般語法來尋找識別值的間距

下列範例會顯示在移除資料時,用來尋找識別值間距的一般語法。

[!附註]

下列 Transact-SQL 指令碼的第一部份專用來說明。您可以執行開頭是下列註解的 Transact-SQL 指令碼:-- Create the img table。

-- Here is the generic syntax for finding identity value gaps in data.
-- The illustrative example starts here.
SET IDENTITY_INSERT tablename ON
DECLARE @minidentval column_type
DECLARE @maxidentval column_type
DECLARE @nextidentval column_type
SELECT @minidentval = MIN($IDENTITY), @maxidentval = MAX($IDENTITY)
    FROM tablename
IF @minidentval = IDENT_SEED('tablename')
   SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('tablename')
   FROM tablename t1
   WHERE $IDENTITY BETWEEN IDENT_SEED('tablename') AND 
      @maxidentval AND
      NOT EXISTS (SELECT * FROM tablename t2
         WHERE t2.$IDENTITY = t1.$IDENTITY + 
            IDENT_INCR('tablename'))
ELSE
   SELECT @nextidentval = IDENT_SEED('tablename')
SET IDENTITY_INSERT tablename OFF
-- Here is an example to find gaps in the actual data.
-- The table is called img and has two columns: the first column 
-- called id_num, which is an increasing identification number, and the 
-- second column called company_name.
-- This is the end of the illustration example.

-- Create the img table.
-- If the img table already exists, drop it.
-- Create the img table.
IF OBJECT_ID ('dbo.img', 'U') IS NOT NULL
   DROP TABLE img
GO
CREATE TABLE img (id_num int IDENTITY(1,1), company_name sysname)
INSERT img(company_name) VALUES ('New Moon Books')
INSERT img(company_name) VALUES ('Lucerne Publishing')
-- SET IDENTITY_INSERT ON and use in img table.
SET IDENTITY_INSERT img ON

DECLARE @minidentval smallint
DECLARE @nextidentval smallint
SELECT @minidentval = MIN($IDENTITY) FROM img
 IF @minidentval = IDENT_SEED('img')
    SELECT @nextidentval = MIN($IDENTITY) + IDENT_INCR('img')
    FROM img t1
    WHERE $IDENTITY BETWEEN IDENT_SEED('img') AND 32766 AND
      NOT    EXISTS (SELECT * FROM img t2
          WHERE t2.$IDENTITY = t1.$IDENTITY + IDENT_INCR('img'))
 ELSE
    SELECT @nextidentval = IDENT_SEED('img')
SET IDENTITY_INSERT img OFF