SQL Server queries all tables and their field properties in the database

Source: Internet
Author: User

The code looks like this:

/*********************************sqlserver queries All tables in the database and their field properties *********************************/SELECT( Case  whenA.colorder= 1  ThenD.nameELSE "'          END) asTable name,--returns null if the table name is the sameA.colorder asfield ordinal, A.name asfield name, ( Case  when ColumnProperty(a.ID, A.name,'isidentity')= 1  Then '√'               ELSE "'          END) asidentification, ( Case  when(SELECT    COUNT(*)                       fromsysobjects--Query primary Key                      WHERE(Nameinch (                                  SELECTname fromsysindexesWHERE(ID=a.id) and(indidinch (                                                  SELECTindid fromSysindexkeysWHERE(ID=a.id) and(colidinch (                                                              SELECTcolid  fromsyscolumns WHERE(ID=a.id) and(Name=( a.name) ))))) and(xtype= 'PK' )                    ) > 0  Then '√'               ELSE "'          END) asPrimary KEY,--Query primary key endB.name astype, A.length asthe number of bytes consumed,ColumnProperty(a.ID, A.name,'PRECISION') aslength,ISNULL(ColumnProperty(a.ID, A.name,' Scale'),0) asNumber of decimal digits, ( Case  whenA.isnullable= 1  Then '√'               ELSE "'          END) asallow NULL,ISNULL(E.text,"') asdefault value,ISNULL(g.[value],"') asField Description fromsyscolumns a Left JOINSystypes b onA.xtype=B.xusertypeINNER JOINsysobjects D ona.ID=d.id andD.xtype= 'U'                                    andD.name<> 'dtproperties'         Left JOINsyscomments E onA.cdefault=e.id Left JOINSys.extended_properties g ona.ID=g.major_id andA.colid=g.minor_idORDER  bya.id, A.colorder;

The query results are as follows:

SQL Server queries all tables and their field properties in the database

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.