Select 只有 table header 中存在于数据字典 ColumnName 中的那些列
Select only those columns from a table header which are present in data dictionary ColumnName
先决条件:所有 table 都是动态的,所以我不能使用列名
我有两个 tables
候选人table:
Table 其中包含需要 selected
的所有列和数据
数据字典:
Table 我只有那些要 select 用于查询的列
现在我想做的是 select 只有数据字典中存在的候选人 table 中的数据和列,并跳过数据字典中不存在的那些数据和列
我试过的是
SELECT ColumnName
INTO #Candidate
FROM DataDictionaryDetail WHERE DataDictionaryId =1
select *
from candidate
where NOT EXISTS (select *from #Candidate)
但这只带来列而不是数据
我还需要一种正确的方法来 select 数据和列
您需要动态 SQL 为此
DECLARE @sql nvarchar(max) = N'
SELECT
'
+ (
SELECT STRING_AGG(QUOTENAME(ColumnName), ',')
FROM DataDictionaryDetail
WHERE DataDictionaryId = 1
) + N'
from candidate;
';
EXEC sp_executesql @sql;
先决条件:所有 table 都是动态的,所以我不能使用列名 我有两个 tables
候选人table: Table 其中包含需要 selected
的所有列和数据数据字典: Table 我只有那些要 select 用于查询的列 现在我想做的是 select 只有数据字典中存在的候选人 table 中的数据和列,并跳过数据字典中不存在的那些数据和列
我试过的是
SELECT ColumnName
INTO #Candidate
FROM DataDictionaryDetail WHERE DataDictionaryId =1
select *
from candidate
where NOT EXISTS (select *from #Candidate)
但这只带来列而不是数据 我还需要一种正确的方法来 select 数据和列
您需要动态 SQL 为此
DECLARE @sql nvarchar(max) = N'
SELECT
'
+ (
SELECT STRING_AGG(QUOTENAME(ColumnName), ',')
FROM DataDictionaryDetail
WHERE DataDictionaryId = 1
) + N'
from candidate;
';
EXEC sp_executesql @sql;