努力寻找从 table 动态 select/join 值的最简单方法

Struggling on the easiest way to dynamically select/join values from a table

我有 table 个变量和值,我将知道哪些参数代码为 select。这个 table 会动态变化。

CREATE TABLE #TreatmentTableVariables (ParameterCode VARCHAR(64), Value varchar(64))
INSERT #TreatmentTableVariables  VALUES ('TripOriginLocationCode','BGY')

然后我有另一个 table 称为 AnalyticsDW.Treatment,其中有一个名为 TripOriginLocationCode 的列,我想 select 来自 AnalyticsDW.Treatment 的那些行,其中 TripOriginLocationCode = 值来自#TreatmentTableVariables。

AnalyticsDW.Treatment 主键为 TreatmentID。

所以我最初使用动态 SQL 来 select 包含在临时 table

中的 TreatmentID 列
SELECT @Columns = SubString (  (  SELECT + ', ' +'t.' + QUOTENAME(Column_name)
from INFORMATION_SCHEMA.columns c
JOIN #TreatmentTableVariables  t ON c.COLUMN_NAME=t.ParameterCode
WHERE Table_name IN ('Treatment','TreatmentProduct') AND TABLE_SCHEMA='AnalyticsDW'
FOR XML PATH ( '' ) ), 1, 1000) 

但我正在努力研究如何仅对 select AnalyticsDW.Treatment 的行执行相同的操作,其中动态列等于来自 #TreatmentTableVariables 的参数代码和来自 #TreatmentTableVariables 的值等于该值该特定列的观察结果。

示例 AnalyticsDW.Treatment 数据:

Declare  AnalyticsDW.Treatment table
(
TreatmentID varchar(100),
TripOriginLocationCode varchar(100),
TripDestinationLocationCode varchar(100)
)

insert into AnalyticsDW.Treatment values
('1','BRG','SLC'),
('2','AHO','BRG')

目标数据集:

Declare  @goal table
(
TripOriginLocationCode varchar(100)
)

insert into @goal values
('BRG')

关于我如何从目标数据集(在动态 sql 查询中)selecting 的示例:

declare @dynamicquery varchar(200)

set @dynamicquery='
select a.*, '+@Columns+' into #CompletePricingtypes2 from #somedataset a 
join AnalyticsDW.Treatment t on a.TreatmentID=t.TreatmentID '

编辑:附加信息

declare
@whereConditions nvarchar(max) = stuff((
    -- here we create where conditions as
    --   paramCode in (itsValues) 
    --   or anotherParamCode in (anotherItsValues) etc.
    select
        'or ' + 'where ' +'t.' +ParameterCode + ' in ('+''''+ltrim([Values]) +''''+') '
    from (
        -- here we create output with two columns: parameter code and
        -- all values associated with that code separated by comma
        select
            t.ParameterCode,
            stuff((
                select
                    ', ' + [Value]
                from #TreatmentTableVariables
                where
                    ParameterCode in (t.ParameterCode)

                FOR XML PATH ('')
            ), 1, 1, '') as [Values]
        from #TreatmentTableVariables t
        where ParameterCode in (select COLUMN_name from INFORMATION_SCHEMA.columns where  Table_name IN ('Treatment') AND TABLE_SCHEMA='AnalyticsDW')
    ) conditions
), 1, 3, '')
print @whereconditions

编辑:有效

 select
    'or ' +   ParameterCode + ' in('+ [Values] +')
       '

    from (
 select distinct
            t.ParameterCode,

           (
                select
                    ', ''' + [Value] + ''''
                from #TreatmentTableVariables
                where
                    ParameterCode in (t.ParameterCode)

            ) as [Values]

        from #TreatmentTableVariables t
        where ParameterCode in (select 
                                COLUMN_name 
                                from INFORMATION_SCHEMA.columns
                                where  Table_name IN ('Treatment') AND TABLE_SCHEMA='AnalyticsDW')
        ) conditions 

UPD。首先,我忘记了 inner select 中的 distinct。

其次,您仍在尝试用单引号将整个 in 条件括起来。你需要

or ParameterCode in ('value1', 'value2', 'value3')

,但你这样做是

or ParameterCode in ('value1, value2, value3') -- look at quotes

第三,你把where作为

的每个or条件
where ParameterCode1 in (...)
or where ParameterCode2 in (...)
or where ParameterCode3 in (...)

将其从 @whereCondition 字符串构造中删除并完成您的查询,就像我在 @selectQuery 变量中所做的那样。


我认为您可以使用真正的动态 sql 查询,即动态构建查询然后执行它(查看下面代码片段中的注释以获取更多信息):

declare
    @whereConditions nvarchar(max) = stuff((
    -- here we create where conditions as
    --   paramCode in (itsValues) 
    --   or anotherParamCode in (anotherItsValues) etc.
    select
        'or ' + ParameterCode + ' in (' + [Values] + ') 
        '
    from (
        -- here we create output with two columns: parameter code and
        -- all values associated with that code separated by comma
        select distinct
            t.ParameterCode,
            stuff((
                select
                    ', ''' + [Value] + ''''
                from #TreatmentTableVariables
                where
                    ParameterCode in (t.ParameterCode)
                FOR XML PATH ('')
            ), 1, 1, '') as [Values]
        from #TreatmentTableVariables t
        where ParameterCode in (
            select 
                COLUMN_name 
            from INFORMATION_SCHEMA.columns 
            where
                table_name = 'Treatment'
                and table_schema ='AnalyticsDW'
        )
    ) conditions
    FOR XML PATH ('') -- !!! this one
), 1, 3, '')

declare
    @selectQuery nvarchar(max) = N'
select
    *
from AnalyticsDW.Treatment
where
    ' + @whereConditions

print @selectQuery
exec sp_executesql @selectQuery