TSQL 将列中的逗号分隔值与逗号分隔参数进行比较
TSQL Compare comma separated values in a column with a comma separated parameter
在我的查询中,我有一个字段 Keywords
,其中包含一个用分号分隔的值列表 ;
在我的 SSRS 报告中,我的一个参数 @Keywords
包含该 Keywords
字段中所有可能的单个值的列表。
How can I pass the multivalued parameter @Keywords
to my query and check if the field Keywords
contains any of the selected parameter values?
我有一个拆分函数,可以将 @Keywords
和 Keywords
分隔到它们各自的关键字项中,但是我如何在我的 Where 语句中执行此检查?
Keywords
例子
corporate finance; compensation; financial markets
@Keywords
示例 Accounting,Accounting and Financial Reporting,Agriculture,airlines,corporate finance,compensation,Offshore drilling,Pharmaceuticals,portfolio,anagement
在这些示例中,@Keywords
包含 corporate finance
和 compensation
,因此我的报告将显示与示例 Keywords
字段关联的记录。
只要@Keywords 包含 Keywords
中的任何值,我们就需要该记录。
您可能需要将其扩展到您的 table 选择我只是有一个概念证明来让它工作,Cross apply 将帮助您在没有任何循环或游标或子查询。
declare @test as table
(
keywords varchar(100)
)
INSERT INTO @test
VALUES ('corporate finance, compensation, financial markets')
declare @keywords varchar(100) ='Accounting,Accounting and Financial Reporting,Agriculture,airlines,corporate finance,compensation,Offshore drilling,Pharmaceuticals,portfolio,anagement'
select i.RecordID as keywords
from @test a
cross apply dbo.ReturnTableOfVarchars(a.keywords) i
INNER JOIN (select * from dbo.ReturnTableOfVarchars (@keywords)) k
ON k.RecordID=i.RecordID
让我知道它是否有效。
在我的查询中,我有一个字段 Keywords
,其中包含一个用分号分隔的值列表 ;
在我的 SSRS 报告中,我的一个参数 @Keywords
包含该 Keywords
字段中所有可能的单个值的列表。
How can I pass the multivalued parameter
@Keywords
to my query and check if the fieldKeywords
contains any of the selected parameter values?
我有一个拆分函数,可以将 @Keywords
和 Keywords
分隔到它们各自的关键字项中,但是我如何在我的 Where 语句中执行此检查?
Keywords
例子
corporate finance; compensation; financial markets
@Keywords
示例 Accounting,Accounting and Financial Reporting,Agriculture,airlines,corporate finance,compensation,Offshore drilling,Pharmaceuticals,portfolio,anagement
在这些示例中,@Keywords
包含 corporate finance
和 compensation
,因此我的报告将显示与示例 Keywords
字段关联的记录。
只要@Keywords 包含 Keywords
中的任何值,我们就需要该记录。
declare @test as table
(
keywords varchar(100)
)
INSERT INTO @test
VALUES ('corporate finance, compensation, financial markets')
declare @keywords varchar(100) ='Accounting,Accounting and Financial Reporting,Agriculture,airlines,corporate finance,compensation,Offshore drilling,Pharmaceuticals,portfolio,anagement'
select i.RecordID as keywords
from @test a
cross apply dbo.ReturnTableOfVarchars(a.keywords) i
INNER JOIN (select * from dbo.ReturnTableOfVarchars (@keywords)) k
ON k.RecordID=i.RecordID
让我知道它是否有效。