How do I list all tables in all databases in SQL Server in a single result set?

后端 未结 16 1253
轮回少年
轮回少年 2020-11-28 07:36

I am looking for T-SQL code to list all tables in all databases in SQL Server (at least in SS2005 and SS2008; would be nice to also apply to SS2000). The catch, however, is

16条回答
  •  孤街浪徒
    2020-11-28 07:45

    I quite like using INFORMATION_SCHEMA for this as I get the DB name for free. That and - realising from @KM post that multiple results sets insert nicely - I came up with:

    select top 0 * 
        into #temp
        from INFORMATION_SCHEMA.TABLES
    
    insert into #temp
        exec sp_msforeachdb 'select * from [?].INFORMATION_SCHEMA.TABLES'
    
    select * from #temp
    
    drop table #temp
    

提交回复
热议问题