Display column information for all tables in SQL Server

Source: Internet
Author: User
Tags join table name

You are working on a program for importing an Excel file from a SQL Server database. To read the columns in the database information, looking for a lot of information from the Internet, and finally summed up the more ideal SQL statements, after the execution of the returned columns are: Table name, column name, column type, column length, column description, whether the primary key, Statement as follows:1 Select sysobjects.name as Tb_name, syscolumns.name as Col_name, systypes.name as Col_type, syscolumns.length as Col_len, Isnull (Sysproperties.value,syscolumns.name) as Col_memo,
2 case when syscolumns.name in
3 (Select primary key =a.name
4 from Syscolumns A
5 Inner Join sysobjects B on a.id=b.id and b.xtype= ' U ' and b.name<> ' dtproperties '
6 where Exists (Select 1 from sysobjects Where xtype= ' Pk "and Name in (
7 Select Name from sysindexes Where indid in (
8 Select indid from Sysindexkeys Where Id = a.id and Colid=a.colid
9))
B.name=sysobjects.name
11)
Then 1 Else 0 End as Is_key
13
From Sysobjects,systypes,syscolumns
Left Join sysproperties on (syscolumns.id = Sysproperties.id and
Syscolumns.colid = sysproperties.smallid)
17
Where (sysobjects.xtype = ' U ' Or sysobjects.xtype = ' V ')
Sysobjects.id = syscolumns.id and Systypes.xtype = Syscolumns.xtype
Systypes.name <> ' Sysname ' and sysobjects.name like '% ' order by Sysobjects.name, Syscolumns.colid

The result is as shown in figure:

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.