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
This is really handy, but I wanted a way to show all user objects, not just tables, so I adapted it to use sys.objects instead of sys.tables
SET NOCOUNT ON
DECLARE @AllTables table (DbName sysname,SchemaName sysname, ObjectType char(2), ObjectName sysname)
DECLARE
@SearchDb nvarchar(200)
,@SearchSchema nvarchar(200)
,@SearchObject nvarchar(200)
,@SQL nvarchar(4000)
SET @SearchDb='%'
SET @SearchSchema='%'
SET @SearchObject='%Something%'
SET @SQL='select ''?'' as DbName, s.name as SchemaName, t.type as ObjectType, t.name as ObjectName
from [?].sys.objects t inner join sys.schemas s on t.schema_id=s.schema_id
WHERE t.type in (''FN'',''IF'',''U'',''V'',''P'',''TF'')
AND ''?'' LIKE '''+@SearchDb+'''
AND s.name LIKE '''+@SearchSchema+'''
AND t.name LIKE '''+@SearchObject+''''
INSERT INTO @AllTables (DbName, SchemaName, ObjectType, ObjectName)
EXEC sp_msforeachdb @SQL
SET NOCOUNT OFF
SELECT * FROM @AllTables ORDER BY DbName, SchemaName, ObjectType, ObjectName
declare @sql nvarchar(max);
set @sql = N'select cast(''master'' as sysname) as db_name, name collate Latin1_General_CI_AI, object_id, schema_id, cast(1 as int) as database_id from master.sys.tables ';
select @sql = @sql + N' union all select ' + quotename(name,'''')+ ', name collate Latin1_General_CI_AI, object_id, schema_id, ' + cast(database_id as nvarchar(10)) + N' from ' + quotename(name) + N'.sys.tables'
from sys.databases where database_id > 1
and state = 0
and user_access = 0;
exec sp_executesql @sql;
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...
2 of 4 - ListServerDatabaseTables
USE [YourDatabase]
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[ListServerDatabaseTables]
(
@SearchDatabases varchar(max) = NULL,
@SearchSchema sysname = NULL,
@SearchTables varchar(max) = NULL,
@ExcludeSystemDatabases bit = 1,
@Sql varchar(max) OUTPUT
)
AS BEGIN
/**************************************************************************************************************************************
* Lists all of the database tables 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
* SearchSchema - Schema name for which to search
* Defaults to null
* SearchTables - Comma delimited list of table 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 KM answered May 21 '10 at 13:33
* 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_TableName sysname
DECLARE @l_DatabaseName sysname
DECLARE @l_Index int
DECLARE @l_UseAndText bit = 0
DECLARE @AllTables table (ServerName sysname, DbName sysname, SchemaName sysname, TableName sysname)
SET @Sql =
'select @@ServerName as ''ServerName'', ''?'' as ''DbName'', s.name as ''SchemaName'', t.name as ''TableName'' ' + char(13) +
'from [?].sys.tables t inner join ' + char(13) +
' sys.schemas s on t.schema_id = s.schema_id '
-- Comma delimited list of database names for which to search
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 @l_UseAndText = 1
END
-- Search schema
IF @SearchSchema IS NOT NULL BEGIN
SET @Sql = @Sql + char(13)
SET @Sql = @Sql + CASE WHEN @l_UseAndText = 1 THEN ' and ' ELSE 'where ' END +
's.name LIKE ''' + @SearchSchema + ''' COLLATE Latin1_General_CI_AS'
SET @l_UseAndText = 1
END
-- Comma delimited list of table names for which to search
IF @SearchTables IS NOT NULL BEGIN
SET @l_CompoundLikeStatement = char(13) + CASE WHEN @l_UseAndText = 1 THEN ' and (' ELSE 'where (' END + char(13)
WHILE LEN(LTRIM(RTRIM(@SearchTables))) > 0 BEGIN
SET @l_Index = CHARINDEX(',', @SearchTables)
IF @l_Index = 0 BEGIN
SET @l_TableName = LTRIM(RTRIM(@SearchTables))
END ELSE BEGIN
SET @l_TableName = LTRIM(RTRIM(LEFT(@SearchTables, @l_Index - 1)))
END
SET @SearchTables = LTRIM(RTRIM(REPLACE(LTRIM(RTRIM(REPLACE(@SearchTables, @l_TableName, ''))), ',', '')))
SET @l_CompoundLikeStatement = @l_CompoundLikeStatement + char(13) + ' t.name like ''$' + @l_TableName + ''' 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 @l_UseAndText = 1
END
IF @ExcludeSystemDatabases = 1 BEGIN
SET @Sql = @Sql + char(13)
SET @Sql = @Sql + case when @l_UseAndText = 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 @AllTables
EXEC sp_msforeachdb @Sql
SELECT * FROM @AllTables ORDER BY DbName COLLATE Latin1_General_CI_AS, SchemaName COLLATE Latin1_General_CI_AS, TableName COLLATE Latin1_General_CI_AS
END
I needed something that I could use to search all my servers using CMS and search by server, DB, schema or table. This is what I found (originally posted by Michael Sorens here: How do I list all tables in all databases in SQL Server in a single result set? ).
SET NOCOUNT ON
DECLARE @AllTables TABLE
(
ServerName NVARCHAR(200)
,DBName NVARCHAR(200)
,SchemaName NVARCHAR(200)
,TableName NVARCHAR(200)
)
DECLARE @SearchSvr NVARCHAR(200)
,@SearchDB NVARCHAR(200)
,@SearchS NVARCHAR(200)
,@SearchTbl NVARCHAR(200)
,@SQL NVARCHAR(4000)
SET @SearchSvr = NULL --Search for Servers, NULL for all Servers
SET @SearchDB = NULL --Search for DB, NULL for all Databases
SET @SearchS = NULL --Search for Schemas, NULL for all Schemas
SET @SearchTbl = NULL --Search for Tables, NULL for all Tables
SET @SQL = 'SELECT @@SERVERNAME
,''?''
,s.name
,t.name
FROM [?].sys.tables t
JOIN sys.schemas s on t.schema_id=s.schema_id
WHERE @@SERVERNAME LIKE ''%' + ISNULL(@SearchSvr, '') + '%''
AND ''?'' LIKE ''%' + ISNULL(@SearchDB, '') + '%''
AND s.name LIKE ''%' + ISNULL(@SearchS, '') + '%''
AND t.name LIKE ''%' + ISNULL(@SearchTbl, '') + '%''
-- AND ''?'' NOT IN (''master'',''model'',''msdb'',''tempdb'',''SSISDB'')
'
-- Remove the '--' from the last statement in the WHERE clause to exclude system tables
INSERT INTO @AllTables
(
ServerName
,DBName
,SchemaName
,TableName
)
EXEC sp_MSforeachdb @SQL
SET NOCOUNT OFF
SELECT *
FROM @AllTables
ORDER BY 1,2,3,4