Sql server 正在努力寻找从表中动态选择/连接值的最简单方法
我有一个变量和值表,我知道要选择哪些参数代码。此表将动态更改Sql server 正在努力寻找从表中动态选择/连接值的最简单方法,sql-server,dynamic-sql,Sql Server,Dynamic Sql,我有一个变量和值表,我知道要选择哪些参数代码。此表将动态更改 CREATE TABLE #TreatmentTableVariables (ParameterCode VARCHAR(64), Value varchar(64)) INSERT #TreatmentTableVariables VALUES ('TripOriginLocationCode','BGY') 然后我有另一个名为AnalyticsDW.Treatment的表,其中有一个名为TripOriginationCode的
CREATE TABLE #TreatmentTableVariables (ParameterCode VARCHAR(64), Value varchar(64))
INSERT #TreatmentTableVariables VALUES ('TripOriginLocationCode','BGY')
然后我有另一个名为AnalyticsDW.Treatment的表,其中有一个名为TripOriginationCode的列,我想从AnalyticsDW.Treatment中选择这些行,其中TripOriginationCode=TreatmentTableVariables中的值
分析治疗具有治疗ID的主键
因此,我最初使用动态SQL选择临时表中包含的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)
但我正在努力研究如何做同样的事情,只选择AnalyticsDW.Treatment的行,其中动态列等于TreatmentTableVariables中的参数代码,TreatmentTableVariables中的值等于该特定列的观察值
样品分析SDW.处理数据:
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查询中从目标数据集中进行选择的示例:
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。首先,我忘记了在内部选择中使用distinct
第二,您仍然试图用单引号将整个包装在条件中。你需要
or ParameterCode in ('value1', 'value2', 'value3')
,但你还是照做
or ParameterCode in ('value1, value2, value3') -- look at quotes
第三,你把每一个或每一个条件放在哪里
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
很好,至少作为一个基础。谢谢有一个问题,在“+[Values]+”中的select”或“+参数code+”中,我如何调整它,使其周围有引号?我一直在尝试调整它,但大部分[Value]都是字符串形式,而不是引号内的[Value]。@AdamSanders,不客气!看看更新的查询版本。您需要用引号将每个[Value]括起来,即在内部选择中进行。正如你所看到的,我在[Value]之前和之后加上了“不得不逃避它,所以它翻了一倍。明白了,还有一个问题。当从TreatmentTableVariables中提取多个参数代码时,我不确定要调整什么。若有一个值,我让它工作,但若我添加另一个值,它表示子查询返回的值超过1个。当子查询在=、!=、=或者当子查询用作表达式时。更新了我的查询并在帖子中添加了说明。我明白你的意思,但由于某种原因,使用新代码仍然会给我相同的错误。