SQL Server resets the automatic number column (ID column)

Source: Internet
Author: User

Two methods:

One is to use truncate

Truncate table name

You can delete all values in the table and reset the id value.

Second, DBCC checkident

DBCC checkident ('table _ name', reseed, new_reseed_value)

For example, DBCC checkident ("bc_pos", reseed, 1) can be used. However, if there is data in the table, the reset value may be faulty if it is smaller than the maximum value.
DBCC checkident ("bc_pos", reseed) can automatically reset the value.

3. Determine whether a table in a segment has an ID column

Available
The objectproperty function determines whether a table has an identity column. Usage:
Select objectproperty (object_id ('table name'), 'tablehasauth ')
If yes, 1 is returned; otherwise, 0 is returned.

4. Determine whether a column is an ID column.

You can use the columnproperty function to determine whether a column has the identity attribute.
Select columnproperty (object_id ('table name'), 'column name', 'isidentity ')
If this column is an identifier column, 1 is returned; otherwise, 0 is returned.

4. query the column names of an identity column in a table.
SQL Server does not have ready-made functions to implement this function. The SQL statements implemented are as follows:
Select column_name from information_schema.columns
Where table_name = 'table name' and columnproperty (
Object_id ('table name'), column_name, 'isidentity ') = 1

5. Identify column reference

If you reference the ID column in an SQL statement, use the keyword identitycol instead.
For example, to query the rows in the previous example with the ID equal to 1,
The following two query statements are equivalent.
Select * From t_test where identitycol = 1
Select * From t_test where

6. Obtain the seed value of the ID column

You can use the ident_seed function. Usage:
Select ident_seed ('table name ')

7. Get the increment of the ID column

You can use the ident_incr function. Usage:
Select ident_incr ('table name ')

8. Obtain the last generated id value in the specified table.

the ident_current function can be used. Usage:
select ident_current ('table name')
note: when the table that contains the ID column is just created and any insert operation is performed, the value obtained by using the ident_current function is the seed value of the ID column, pay special attention to this when developing database applications.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.