Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Server:跨数据库查找唯一表的列表_Sql Server_Database_Sql Server 2012_Database Schema_Information Schema - Fatal编程技术网

Sql server SQL Server:跨数据库查找唯一表的列表

Sql server SQL Server:跨数据库查找唯一表的列表,sql-server,database,sql-server-2012,database-schema,information-schema,Sql Server,Database,Sql Server 2012,Database Schema,Information Schema,我的SQLServer2012机器上大约有21个数据库,理想情况下应该有相同的表列表,但它们没有 假设: 数据库1有表A、B、C、D、E 数据库2有表A、B、C、D、Z 数据库3有表A、B、C、Y、Z 数据库4有表A、B、X、Y、Z 最后一个查询的输出必须是表列表,如a、B、C、D、E、X、Y、Z 我知道可以通过连接每个数据库的sys.tables来获取这些信息,但我不确定应该使用哪个连接以及如何使用 任何起点都将受到欢迎您可以使用union: select d.* from ((selec

我的SQLServer2012机器上大约有21个数据库,理想情况下应该有相同的表列表,但它们没有

假设:

  • 数据库1有表A、B、C、D、E
  • 数据库2有表A、B、C、D、Z
  • 数据库3有表A、B、C、Y、Z
  • 数据库4有表A、B、X、Y、Z
最后一个查询的输出必须是表列表,如a、B、C、D、E、X、Y、Z

我知道可以通过连接每个数据库的
sys.tables
来获取这些信息,但我不确定应该使用哪个连接以及如何使用


任何起点都将受到欢迎

您可以使用
union

select d.*
from ((select table_name from db1.information_schema.tables) union
      (select table_name from db2.information_schema.tables) union
      . . .
      (select table_name from db21.information_schema.tables) 
     ) d;

实际上,子查询并不是必需的,但是如果您想做一些事情,例如计算每个表出现的次数,子查询非常方便。

您可以使用
union

select d.*
from ((select table_name from db1.information_schema.tables) union
      (select table_name from db2.information_schema.tables) union
      . . .
      (select table_name from db21.information_schema.tables) 
     ) d;

实际上,子查询并不是必需的,但是如果您想做一些事情,例如计算每个表出现的次数,子查询非常方便。

如果我正确理解了您的问题

您想列出所有数据库的所有表

有一个非常简单的脚本可以完成这个任务

作为地板:

sp_msforeachdb 'select "?" AS db, * from [?].sys.tables'
以及仅获取表列表:

sp_msforeachdb 'select "?" AS db, name from [?].sys.tables'

如果我正确理解了你的问题,希望这有帮助

您想列出所有数据库的所有表

有一个非常简单的脚本可以完成这个任务

作为地板:

sp_msforeachdb 'select "?" AS db, * from [?].sys.tables'
以及仅获取表列表:

sp_msforeachdb 'select "?" AS db, name from [?].sys.tables'

希望这对那些将来偶然发现这一点的人有所帮助

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 distinct tablename 
FROM    @AllTables

只是为了那些在未来偶然发现这一点的人

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 distinct tablename 
FROM    @AllTables

是我发现的相似之处,但我需要对多个数据库执行相同的操作是我发现的相似之处,但我需要对多个数据库执行相同的操作跨数据库唯一表跨数据库唯一表。