Sql server 与sql server 2012中的其他相同数据库相比,用于查找一个数据库中缺少的索引的脚本

Sql server 与sql server 2012中的其他相同数据库相比,用于查找一个数据库中缺少的索引的脚本,sql-server,Sql Server,我有两个数据库,分别是test1和test2,两者都是相同的,即都有相同的表和相同的名称。但是test1在表上有一些索引,而这些索引在test2数据库中缺失。与sql server 2012中的test1数据库相比,如何编写脚本来查找test2数据库中缺失的索引。我尝试了下面的脚本,但它显示了所有索引。但我只希望在中缺少索引与test1数据库相比,test2数据库 select * from (select s1.name as SchemaName

我有两个数据库,分别是test1和test2,两者都是相同的,即都有相同的表和相同的名称。但是test1在表上有一些索引,而这些索引在test2数据库中缺失。与sql server 2012中的test1数据库相比,如何编写脚本来查找test2数据库中缺失的索引。我尝试了下面的脚本,但它显示了所有索引。但我只希望在中缺少索引与test1数据库相比,test2数据库

select *
  from
       (select s1.name           as SchemaName
              ,t1.name           as TableName
              ,c1.name           as ColumnName
              ,i1.name           as IndexName
              ,i1.index_id
              from test1.sys.schemas s1
          join test1.sys.tables t1
            on t1.schema_id = s1.schema_id
          join test1.sys.columns c1
            on t1.object_id = c1.object_id
          join test1.sys.types ty1
            on ty1.system_type_id = c1.system_type_id
           and ty1.user_type_id = c1.user_type_id
          join test1.sys.index_columns ic1
            on ic1.object_id = c1.object_id
           and ic1.column_id = c1.column_id
          join test1.sys.indexes i1
            on i1.object_id = ic1.object_id
           and i1.index_id = ic1.index_id) r1
  full outer join
       (select s1.name           as SchemaName
              ,t1.name           as TableName
              ,c1.name           as ColumnName
              ,i1.name           as IndexName
              ,i1.index_id
          from test2.sys.schemas s1
          join test2.sys.tables t1
            on t1.schema_id = s1.schema_id
          join test2.sys.columns c1
            on t1.object_id = c1.object_id
          join test2.sys.types ty1
            on ty1.system_type_id = c1.system_type_id
           and ty1.user_type_id = c1.user_type_id
          join test2.sys.index_columns ic1
            on ic1.object_id = c1.object_id
           and ic1.column_id = c1.column_id
          join test2.sys.indexes i1
            on i1.object_id = ic1.object_id
           and i1.index_id = ic1.index_id) r2 
    on r1.SchemaName = r2.SchemaName
   and r1.TableName = r2.TableName
   and r1.ColumnName = r2.ColumnName
   and r1.IndexName = r2.IndexName

使用
左外部联接
并在
末尾添加,其中r2.IndexName为null