Declare @ Table_name varchar (60)
Set @ Table_name = '';
If Exists (Select top 1 1 from sysobjects
Where objectproperty (id, 'tablehasauth') = 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
Determine whether the Table has an auto-increment column (Identity column) and check the relevant data of the auto-increment 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, -- table name
SC. name Iden_Column_name, -- auto-increment field name
Ident_current (so. name) curr_value, -- current value of the auto-increment Field
Ident_incr (so. name) incr_value, -- auto-increment field Growth Value
Ident_seed (so. name) seed_value -- seed value of the auto-increment Field
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)
Reference:
DBCC CHECKIDENT
Check the current Identifier value of the specified table. If necessary, correct the Identifier value.
Syntax
DBCC CHECKIDENT
('Table_name'
[,{NORESEED
| {RESEED [, New_reseed_value]}
}
]
)
Parameters
'Table_name'
Is the name of the table whose current identity value is to be checked. The table name must comply with the identifier rules. For more information, seeIdentifier. The specified table must contain an ID column.
NORESEED
Specifies that the current ID value should not be corrected.
RESEED
Specifies that the current ID value should be corrected.
New_reseed_value
Is the value to be used when the value is re-assigned in the ID column.
Note
If necessary, dbcc checkident will correct the current ID value of the column. However, if the ID column is created using the not for replication clause (in the create table or alter table statement), the current ID value is NOT corrected.
If the primary key or unique key constraint exists in the ID column, invalid ID information may cause error 2627.
The specific correction to the current Identifier value depends on the parameter specification.
Dbcc checkident statement |
Id correction |
Dbcc checkident ('Table_name', NORESEED) |
The current ID value is not reset. Dbcc checkident returns a report that specifies the current ID value and the expected id value. |
Dbcc checkident ('Table_name') Or Dbcc checkident ('Table_name', RESEED) |
If the current ID value of the table is smaller than the maximum id value stored in the column, reset it with the maximum value in the column. |
Dbcc checkident ('Table_name', RESEED,New_reseed_value) |
The current value is setNew_reseed_value. If the row is not inserted into the table after the table is created, the first row inserted after dbcc checkident is executed will useNew_reseed_valueAs the identifier. Otherwise, the next inserted row usesNew_reseed_value+ 1. IfNew_reseed_valueThe value of is less than the maximum value in the identification column. When you reference the table later, error 2627 is generated. |
The current ID value can be greater than the maximum value in the table. In this case, dbcc checkident does not automatically reset the current ID value. To reset the current ID value when the current ID value is greater than the maximum value in the column, use either of the following methods:
- Run dbcc checkident ('Table_name', NORESEED) to determine the current maximum value in the column, and then use dbcc checkident ('Table_name', RESEED,New_reseed_value) Statement to specify this valueNew_reseed_value.
- SetNew_reseed_valueTo execute dbcc checkident ('Table_name', RESEED,New_reseed_value), And then run dbcc checkident ('Table_name', RESEED ).
Result set
Whether or not any options are specified (for tables that contain the ID column; the following example usesPubsDatabaseJobsTable), dbcc checkident returns the following result set (the value may change ):
Checking identity information: current identity value '14', current column value '14'.DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Permission
Dbcc checkident permission is granted to the table owner by default,SysadminFixed server roles andDb_ownerMembers of a fixed database role cannot be transferred.
Example A. Reset the current ID value if necessary
The following example is reset if necessary.JobsThe current Identifier value of the table.
USE pubsGODBCC CHECKIDENT (jobs)GO
B. report the current ID value
Example reportJobsThe current ID value in the table. If the id value is incorrect, it is not corrected.
USE pubsGODBCC CHECKIDENT (jobs, NORESEED)GO
C. Force the current ID value to 30
Force the following exampleJobsThe current ID value in the table is 30.
USE pubsGODBCC CHECKIDENT (jobs, RESEED, 30)GO