MSSQL 自增列的資訊獲取

2022-09-17 08:33:10 字數 3756 閱讀 2371

select ident_current('tablename')--標識當前值

/*----------------------------------------

3(所影響的行數為 1 行)

*/select ident_incr('tablename')--標識增量

/*                                         

----------------------------------------

1(所影響的行數為 1 行)

*/select ident_seed('tablename')--標識種子

/*----------------------------------------

1(所影響的行數為 1 行)

*/declare @table_name varchar(60)

set @table_name = '';

if exists(select top 1 1 from sysobjects

where objectproperty(id, 'tablehasidentity') = 1

and upper(name) = upper(@table_name)

)select 1

else select 0

-- or

if exists(select top 1 1 from sysobjects so

where so.xtype = 'u'

and upper(so.name) = upper(@table_name)

and exists(select top 1 1 from syscolumns sc

where sc.id = so.id

and columnproperty(sc.id, sc.name, 'isidentity') = 1))

select 1

else select 0

判斷table是否存在自增列(identity column),並查出自增列相關資料:

declare @table_name varchar(60)

set @table_name = '';

declare @table_name varchar(60)

set @table_name = 'zy_cost_list';

select so.name table_name, --表名字

sc.name iden_column_name, --自增欄位名字

ident_current(so.name) curr_value, --自增字段當前值

ident_incr(so.name) incr_value, --自增字段增長值

ident_seed(so.name) seed_value --自增字段種子值

from sysobjects so

inner join syscolumns sc

on so.id = sc.id

and columnproperty(sc.id, sc.name, 'isidentity') = 1

where upper(so.name) = upper(@table_name)

資料引用:

dbcc checkident

檢查指定表的當前標識值,如有必要,還對標識值進行更正。

dbcc checkident

( 'table_name'

[ , }]

)'table_name'

是要對其當前標識值進行檢查的表名。表名必須符合識別符號規則。有關更多資訊,請參見使用識別符號。指定的表必須包含標識列。

noreseed

指定不應更正當前標識值。

reseed

指定應該更正當前標識值。

new_reseed_value

是在標識列中重新賦值時要使用的值。

如有必要,dbcc checkident 會更正列的當前標識值。然而,如果標識列是使用 not for replication 子句(在 create table 或 alter table 語句中)建立的,則不更正當前標識值。

如果標識列上有主鍵或唯一鍵約束,無效標識資訊可能會導致錯誤資訊 2627。

對當前標識值所做的具體更正取決於引數規範。

dbcc checkident 語句     所做的標識更正

dbcc checkident ('table_name', noreseed)     不重置當前標識值。dbcc checkident 返回乙個報表,它指明當前標識值和應有的標識值。

dbcc checkident ('table_name') 或

dbcc checkident ('table_name', reseed)     如果表的當前標識值小於列中儲存的最大標識值,則使用標識列中的最大值對其進行重置。

dbcc checkident ('table_name', reseed, new_reseed_value)     當前值設定為 new_reseed_value。如果自建立表後沒有將行插入該錶,則在執行 dbcc checkident 後插入的第一行將使用 new_reseed_value 作為標識。否則,下乙個插入的行將使用 new_reseed_value + 1。如果 new_reseed_value 的值小於標識列中的最大值,以後引用該錶時將產生 2627 號錯誤資訊。

當前標識值可以大於表中的最大值。在此情況下,dbcc checkident 並不自動重置當前標識值。若要在當前標識值大於列中的最大值時對當前標識值進行重置,請使用兩種方法中的任意一種:

執行 dbcc checkident ('table_name', noreseed) 以確定列中的當前最大值,然後使用 dbcc checkident ('table_name', reseed, new_reseed_value) 語句將該值指定為 new_reseed_value。

將 new_reseed_value 置為很小值來執行 dbcc checkident ('table_name', reseed, new_reseed_value),然後執行 dbcc checkident ('table_name', reseed)。

不管是否指定任何選項(針對於包含標識列的表;下例使用 pubs 資料庫的 jobs 表),dbcc checkident 返回以下結果集(值可能會有變化):

checking identity information: current identity value '14', current column value '14'.

dbcc execution completed. if dbcc printed error messages, contact your system administrator.

dbcc checkident 許可權預設授予表所有者、sysadmin 固定伺服器角色和 db_owner 固定資料庫角色的成員且不可轉讓。

a. 如有必要,重置當前標識值

下例在必要的情況下重置 jobs 表的當前標識值。

use pubs

godbcc checkident (jobs)

gob. 報告當前標識值

下例報告 jobs 表中的當前標識值;如果該標識值不正確,並不對其進行更正。

use pubs

godbcc checkident (jobs, noreseed)

goc. 強制當前標識值為 30

下例強制 jobs 表中的當前標識值為 30。

use pubs

godbcc checkident (jobs, reseed, 30)

go

MSSql關閉自增列

在對已經建好表結構的表抽取資料的時候,突然報錯,根據error發現,不能顯式插入有自增列的值。於是搜尋後,用 set identity insert tmp on set identity insert tmp off 把自增列關閉再差,就ok了,附上 create table tmp id int...

重置SQLSERVER表的自增列,讓自增列重新計數

sql的自增列挺好用,只是開發過程中一旦刪除資料,標識列就不連續了 寫起來 也很鬱悶,所以查閱了一下標識列重置的方法 發現可以分為三種 刪除原表資料,並重置自增列 truncate table tablename truncate方式也可以重置自增字段 重置表的自增欄位,保留資料 dbcc chec...

重置SQLSERVER表的自增列,讓自增列重新計數

sql的自增列挺好用,只是開發過程中一旦刪除資料,標識列就不連續了 寫起來 也很鬱悶,所以查閱了一下標識列重置的方法 發現可以分為三種 刪除原表資料,並重置自增列 truncate table tablename truncate方式也可以重置自增字段 重置表的自增欄位,保留資料 dbcc chec...