Determine whether the table has an auto-increment column (

Source: Internet
Author: User

SQL Server auto-increment Column

Determine whether the table has an auto-increment column (identity column ):

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)

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.