如何在SQL Server数据库的多个表中找到匹配的列?

时间:2018-12-18 23:19:18

标签: sql-server sqlite join multiple-columns

如何在SQL Server数据库的多个表中查找匹配的列?

我有一个包含30多个表的SQL Server数据库,是否有一种方法可以过滤和/或生成表中的公共列列表,而不是手动进行遍历?

也许有些代码?

6 个答案:

答案 0 :(得分:2)

如评论中所述,您想使用系统表sys.columns来实现目标。

此查询列出了数据库中不止一次出现的所有列名,以及出现的次数:

SELECT name, COUNT(*) 
FROM sys.columns 
GROUP BY name
HAVING COUNT(*) > 1

您可以将结果与sys.tables合并以恢复相应的表,例如:

SELECT
    C.name AS ColumnName,
    T.name AS TableName
FROM
    (SELECT name FROM sys.columns GROUP BY name HAVING COUNT(*) > 1 ) A
    JOIN sys.columns C on C.name = A.name
    JOIN sys.tables T ON T.object_id = C.object_id
ORDER BY
    C.name,
    T.name

答案 1 :(得分:1)

我会这样简单地使用

<Router AppAssembly=typeof(Program).Assembly FallbackComponent="typeof(Error404)" >

答案 2 :(得分:0)

通过此查询,您在数据库的不同表中具有相同名称的所有列:

select object_name(c.id) + '.' + c.name + ' ' 
        + t.name
        + case when t.xtype = t.xusertype then '' else '[' + tr.name end 
        + case 
            when tr.name in ('bit', 'tinyint', 'smallint', 'int', 'bigint', 
                            'float', 'money', 'smallmoney', 'real', 'date', 'time', 
                            'datetime', 'datetime2', 'smalldatetime', 'timestamp')
            then ''
            else 
                case when isNull(c.prec,0)=0 then ''
                else '(' 
                    + case when c.prec = - 1 then 'MAX' else cast(c.prec as varchar) end
                    + case when c.scale is null then '' else ',' + cast(c.scale as varchar) end
                    + ')'
                end
          end
        + case when t.xtype = t.xusertype then '' else ']' end
        + case when t.collationId <> c.collationId
             then ' collate ' + c.collation collate Latin1_General_BIN else '' end
        + case c.isnullable when 0 then ' not null' else '' end
        + case c.colstat
            when 1 then ' identity(' + Cast(Ident_seed(o.name) as varchar)
                      + ',' + cast(Ident_incr(o.name) as varchar) + ')'
            else ''
          end
        + case when cm.text is null then ''
          else ' default '
            + case when patindex('% as %', cm.text) > 0
              then rtrim(substring(cm.text, patindex('% as %', cm.text) + 4, len(cm.text)))
              else substring(cm.text, 2, len(cm.text) - 2)
              end
          end
        as Columns
from syscolumns c
join systypes t on (t.xusertype = c.xusertype)
left join systypes tr
    on (tr.xtype = t.xtype and tr.xusertype = t.xtype)
join sysobjects o
    on (o.id = c.id)
left join syscomments cm
    on (cm.id = c.cdefault)
where c.name in (
        select cl.name
        from syscolumns cl
        join sysobjects ob on (cl.id = ob.id and ob.xtype = 'U')
        group by cl.name
        having count(*) > 1
        )
order by c.name

答案 3 :(得分:0)

这将起作用:

select COLUMN_NAME from ALL_TAB_COLS where TABLE_NAME = 'table1name'
intersect
select COLUMN_NAME from ALL_TAB_COLS where TABLE_NAME = 'table2name';
intersect
...
...
..
...
select COLUMN_NAME from ALL_TAB_COLS where TABLE_NAME = 'table30name';

等效的SQL Server:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table1'
intersect
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table2'
intersect
.....
....
...
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table30';

答案 4 :(得分:0)

下面是查询,它将为您提供“列”所在的所有表的列表。

SELECT c.name AS ColName, t.name AS TableName FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id WHERE c.name LIKE '%ColumnName%';

基于此,您可以继续执行要执行的任务。

如果有帮助,请竖起大拇指。

答案 5 :(得分:0)

从syscolumns s1中选择名称作为“ Column_Name”,其中id = object_id('table1')和
       存在(从syscolumns s2中选择1,其中s2.name = s1.name和s2.id = object_id('table2'))

它将返回匹配的列