Obtain the sqlserver table structure (field, primary key, foreign key, increment, description)

Source: Internet
Author: User

1. Get basic field attributes of the table

-- Obtain the table structure in sqlserver
Select Syscolumns. Name, policypes. Name, syscolumns. isnullable,
Syscolumns. Length
From Syscolumns, policypes
Where Syscolumns. xusertype = Policypes. xusertype
And Syscolumns. ID =   Object_id ( ' Your table name ' )

Running Effect

2. If you want to obtain the description of a field

-- Obtain the primary key and description of the table structure in sqlserver.
Declare   @ Table_name   As   Varchar ( Max )
Set   @ Table_name   =   ' Your table name '  
Select SYS. Columns. Name, SYS. types. Name, SYS. Columns. max_length, SYS. Columns. is_nullable,
( Select   Count ( * ) From SYS. identity_columns Where SYS. identity_columns. Object_id   = SYS. columns. Object_id   And SYS. Columns. column_id = SYS. identity_columns.column_id) As Is_identity,
( Select Value From SYS. extended_properties Where SYS. extended_properties.major_id = SYS. columns. Object_id   And SYS. extended_properties.minor_id = SYS. Columns. column_id) As Description
From SYS. columns, SYS. Tables, SYS. Types Where SYS. columns. Object_id   = SYS. Tables. Object_id   And SYS. Columns. system_type_id = SYS. types. system_type_id And SYS. Tables. Name = @ Table_name   Order   By SYS. Columns. column_id

Running Effect

3. Independently query incremental fields of the table
-- Separately query the incremental field of the table
Select   [ Name ]   From Syscolumns Where  
ID = Object_id (N ' Your table name ' ) And   Columnproperty (ID, name, ' Isidentity ' ) = 1

Running Effect

4. Obtain the table's primary and Foreign keys
-- Obtain the table's primary and foreign key constraints
Exec Sp_helpconstraint ' Your table name ' ;

Running Effect

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.