Generics t-sql-组合表元数据和列值

Generics t-sql-组合表元数据和列值,generics,sql-server-2012,dynamic-sql,Generics,Sql Server 2012,Dynamic Sql,我试图用T-SQL编写一个查询,如下所示: select * from (select t.name tablename from sys.columns c join sys.tables t on c.object_id = t.object_id where c.name like 'column1' ) candidatetables where tablename.column2 = 3 select top(1) 'Table1' as TableName from [Table1

我试图用T-SQL编写一个查询,如下所示:

select *
from
(select t.name tablename
from sys.columns c join sys.tables t on c.object_id = t.object_id
where c.name like 'column1'
) candidatetables
where tablename.column2 = 3
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 
问题出在最后一个过滤器中。我得到这个错误“多部分标识符”tablename.column2“无法绑定”


此查询应获取在其架构中具有列“column1”的表,对于这些表,名为“column2”的列的值等于3,该列肯定存在于所有表中。是否可以以不同的方式写入最后一个筛选器以完成此操作?

您的查询不会查询您找到的表。您必须动态构建一个查询,查询找到的每个表,然后使用
union all
组合结果

试试这个:

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)')

exec (@SQL)
上面的代码将生成并执行一个如下所示的查询:

select *
from
(select t.name tablename
from sys.columns c join sys.tables t on c.object_id = t.object_id
where c.name like 'column1'
) candidatetables
where tablename.column2 = 3
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 

您的查询没有查询您找到的表。您必须动态构建一个查询,查询找到的每个表,然后使用
union all
组合结果

试试这个:

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)')

exec (@SQL)
上面的代码将生成并执行一个如下所示的查询:

select *
from
(select t.name tablename
from sys.columns c join sys.tables t on c.object_id = t.object_id
where c.name like 'column1'
) candidatetables
where tablename.column2 = 3
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 

那么,您希望查询的输出是一个表名列表,还是希望表中的数据?首先,我想获取模式中有column1的表以及其column2的给定值。然后我想查询这些表。假设我有表T1(第1列,第2列),T2(第1列,第2列),T3(第1列,第2列),T4(第4列,第2列),并且假设T1.column2=3,T2.column2=1000,T3.column2=3。首先,我将得到T1、T2和T3,因为它们的模式中都有column1。但在下一步中,我想获得表T1和T3,因为这些表的字段column2的值是3。最后,我想从这些表中查询其他字段。希望这更清楚。那么您希望查询的输出是一个表名列表,还是希望表中的数据?首先,我希望得到架构中有column1的表,以及column2的给定值。然后我想查询这些表。假设我有表T1(第1列,第2列),T2(第1列,第2列),T3(第1列,第2列),T4(第4列,第2列),并且假设T1.column2=3,T2.column2=1000,T3.column2=3。首先,我将得到T1、T2和T3,因为它们的模式中都有column1。但在下一步中,我想获得表T1和T3,因为这些表的字段column2的值是3。最后,我想从这些表中查询其他字段。希望这更清楚。这很好,很有效。谢谢还有两个问题。1.为什么需要将查询结果转换为xml?2.如果我有两个动态SQL(1和2),如示例中的一个,用于过滤不同的表,那么是否可以编写一个查询,将来自SQL1的表与来自SQL2的表连接起来,并从这些表中选择一些数据?XML内容是字符串连接技巧,用于从多行构建一个字符串。第二部分我不明白你想要什么。请更新您的问题或提出新问题。很可能您必须使用此命令的结果来构建一个新的动态SQL查询,该查询可以满足您的需要。我读过关于for xml的文章,但我想知道是否存在其他替代方案。Thanks@user1744925另一种选择是使用游标在行上循环,并在循环中构建动态SQL。这很好,很有效。谢谢还有两个问题。1.为什么需要将查询结果转换为xml?2.如果我有两个动态SQL(1和2),如示例中的一个,用于过滤不同的表,那么是否可以编写一个查询,将来自SQL1的表与来自SQL2的表连接起来,并从这些表中选择一些数据?XML内容是字符串连接技巧,用于从多行构建一个字符串。第二部分我不明白你想要什么。请更新您的问题或提出新问题。很可能您必须使用此命令的结果来构建一个新的动态SQL查询,该查询可以满足您的需要。我读过关于for xml的文章,但我想知道是否存在其他替代方案。Thanks@user1744925另一种选择是使用游标在行上循环,并在循环中构建动态SQL。