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

前端 未结 13 597
被撕碎了的回忆
被撕碎了的回忆 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:55

    try the below query

    DECLARE @Query VARCHAR(max) 
    SELECT @Query = 'USE ? SELECT ''?'' AS DataBaseName,
                                    sys.columns.name AS ColumnName  ,
                                    sys.tables.name  AS TableName   ,
                                    schema_name (sys.tables.schema_Id) AS schemaName
                             FROM sys.columns
                             JOIN sys.tables 
                  ON sys.columns.object_id = sys.tables.object_id
                  WHERE sys.columns.name = ''id'' '
    EXEC SP_MSFOREACHDB @Query
    

    gives list of tables containing ID column from all databases.

提交回复
热议问题