Displays the column information of all tables in SQL SERVER.

Source: Internet
Author: User

Displays the column information of all tables in SQL SERVER.

I am working on a program for importing an Excel file from the SQL server database. I want to read the column information in the database, find a lot of information from the Internet, and finally summarize the ideal SQL statement, after execution, the returned columns are: Table Name, column name, column type, column length, column description, and whether the primary key is used. The statement is as follows:


SELECT SysObjects. Name as tb_name, SysColumns. Name as col_name, policypes. Name as col_type, SysColumns. Length as col_len, isnull (SysProperties. Value, SysColumns. Name) as col_memo,
Case when SysColumns. name in
(Select primary key = a. name
FROM syscolumns
Inner join sysobjects B on a. id = B. id and B. xtype = 'U' and B. name <> 'dtproperties'
Where exists (SELECT 1 FROM sysobjects where xtype = 'pk' and name in (
SELECT name FROM sysindexes WHERE indid in (
SELECT indid FROM sysindexkeys WHERE id = a. id AND colid = a. colid
And B. name = SysObjects. Name
Then 1 else 0 end as is_key

FROM SysObjects, policypes, SysColumns
Left join SysProperties ON (Syscolumns. Id = Sysproperties. Id AND
Syscolumns. Colid = Sysproperties. Smallid)

WHERE (Sysobjects. Xtype = 'U' OR Sysobjects. Xtype = 'V ')
AND Sysobjects. Id = Syscolumns. Id AND policypes. XType = Syscolumns. XType
AND policypes. Name <> 'sysname' AND Sysobjects. Name Like '%' ORDER By SysObjects. Name, SysColumns. colid

If you only want to operate a table, replace the last % with the table name.

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.