How to find column names for all tables in all databases in SQL Server

前端 未结 13 598
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 04:30

I want to find all column names in all tables in all databases. Is there a query that can do that for me? The database is Microsoft SQL Server 2000.

13条回答
  •  迷失自我
    2020-11-28 04:58

    I just realized that the following query would give you all column names from the table in your database (SQL SERVER 2017)

    SELECT DISTINCT NAME FROM SYSCOLUMNS 
    ORDER BY Name 
    

    OR SIMPLY

    SELECT Name FROM SYSCOLUMNS
    

    If you do not care about duplicated names.

    Another option is SELECT Column names from INFORMATION_SCHEMA

    SELECT DISTINCT column_name  FROM INFORMATION_SCHEMA.COLUMNS
    ORDER BY column_name
    

    It is usually more interesting to have the TableName as well as the ColumnName ant the query below does just that.

    SELECT 
       Object_Name(Id) As TableName,
       Name As ColumnName
    FROM SysColumns
    

    And the results would look like

      TableName    ColumnName
    0    Table1    column11
    1    Table1    Column12
    2    Table2    Column21
    3    Table2    Column22
    4    Table3    Column23
    

提交回复
热议问题