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

后端 未结 16 1216
轮回少年
轮回少年 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:43

    I realize this is a very old thread, but it was very helpful when I had to put together some system documentation for several different servers that were hosting different versions of Sql Server. I ended up creating 4 stored procedures which I am posting here for the benefit of the community. We use Dynamics NAV so the two stored procedures with NAV in the name split the Nav company out of the table name. Enjoy...

    1 of 4 - ListServerDatabases

    USE [YourDatabase]
    GO
    
    /****** Object:  StoredProcedure [pssi].[ListServerDatabases]    Script Date: 10/3/2017 8:56:45 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    ALTER PROC [dbo].[ListServerDatabases]
    (
        @SearchDatabases varchar(max) = NULL,  
        @ExcludeSystemDatabases bit = 1,
        @Sql varchar(max) OUTPUT
    )
    AS BEGIN
    
    /**************************************************************************************************************************************
    * Lists all of the databases for a given server.
    *   Parameters
    *       SearchDatabases - Comma delimited list of database names for which to search - converted into series of Like statements
    *                         Defaults to null  
    *       ExcludeSystemDatabases - 1 to exclude system databases, otherwise 0
    *                                Defaults to 1
    *       Sql - Output - the stored proc generated sql
    *
    *   Adapted from answer by 
    *   From: How do I list all tables in all databases in SQL Server in a single result set?
    *   Link: https://stackoverflow.com/questions/2875768/how-do-i-list-all-tables-in-all-databases-in-sql-server-in-a-single-result-set
    *
    **************************************************************************************************************************************/
    
        SET NOCOUNT ON
    
        DECLARE @l_CompoundLikeStatement varchar(max) = ''
        DECLARE @l_DatabaseName sysname
    
        DECLARE @l_Index int
    
        DECLARE @lUseAndText bit = 0
    
        DECLARE @l_AllDatabases table (ServerName sysname, DbName sysname)
    
        SET @Sql = 
            'select @@ServerName as ''ServerName'', ''?'' as ''DbName'''
    
        IF @SearchDatabases IS NOT NULL BEGIN
            SET @l_CompoundLikeStatement = char(13) + 'where (' + char(13)
            WHILE LEN(LTRIM(RTRIM(@SearchDatabases))) > 0 BEGIN
                SET @l_Index = CHARINDEX(',', @SearchDatabases)
                IF @l_Index = 0 BEGIN
                    SET @l_DatabaseName = LTRIM(RTRIM(@SearchDatabases))
                END ELSE BEGIN
                    SET @l_DatabaseName = LTRIM(RTRIM(LEFT(@SearchDatabases, @l_Index - 1)))
                END
    
                SET @SearchDatabases = LTRIM(RTRIM(REPLACE(LTRIM(RTRIM(REPLACE(@SearchDatabases, @l_DatabaseName, ''))), ',', '')))
                SET @l_CompoundLikeStatement = @l_CompoundLikeStatement + char(13) + ' ''?'' like ''' + @l_DatabaseName + '%'' COLLATE Latin1_General_CI_AS or '
            END
    
            -- Trim trailing Or and add closing right parenthesis )
            SET @l_CompoundLikeStatement = LTRIM(RTRIM(@l_CompoundLikeStatement))
            SET @l_CompoundLikeStatement = LEFT(@l_CompoundLikeStatement, LEN(@l_CompoundLikeStatement) - 2) + ' )'
    
            SET @Sql = @Sql + char(13) +
                @l_CompoundLikeStatement
    
            SET @lUseAndText = 1
        END
    
        IF @ExcludeSystemDatabases = 1 BEGIN
            SET @Sql = @Sql + char(13)
            SET @Sql = @Sql + case when @lUseAndText = 1 THEN '  and ' ELSE 'where ' END +
                '''?'' not in (''master'' COLLATE Latin1_General_CI_AS, ''model'' COLLATE Latin1_General_CI_AS, ''msdb'' COLLATE Latin1_General_CI_AS, ''tempdb'' COLLATE Latin1_General_CI_AS)' 
        END
    
    /*  PRINT @Sql  */
    
        INSERT INTO @l_AllDatabases 
        EXEC sp_msforeachdb @Sql
    
        SELECT * FROM @l_AllDatabases ORDER BY DbName
    END
    

提交回复
热议问题