sql 查找具有指定列名称和值的所有表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 查找具有指定列名称和值的所有表相关的知识,希望对你有一定的参考价值。

/*
Find all tables with certain column name and values for that column name

https://stackoverflow.com/questions/12882616/t-sql-combine-table-metadata-and-column-values
http://sqlfiddle.com/#!6/dcbf6/1/0
*/
/*
Builds query similar to this:

select top(1) 'Table1' as TableName
from [Table1]
where Column2 = 3
union all
select top(1) 'Table2' as TableName
from [Table2] 
where Column2 = 3
union all 
select top(1) 'Table3' as TableName
from [Table3] 
where Column2 = 3 

*/
declare @Col2Value int = 3
declare @SQL nvarchar(max)

select @SQL = 
(
  select 'union all '+
         'select top(1) '''+t.name+''' as TableName '+
         'from '+quotename(t.name)+' '+
         'where Column2 = '+cast(@Col2Value as nvarchar(10))+' '
  from sys.columns c
    inner join sys.tables t
      on c.object_id = t.object_id
  where c.name = 'Column1'
  for xml path(''), type
).value('substring(./text()[1], 11)', 'nvarchar(max)')

--print @SQL
exec (@SQL)

以上是关于sql 查找具有指定列名称和值的所有表的主要内容,如果未能解决你的问题,请参考以下文章