【问题标题】:TSQL Compare comma separated values in a column with a comma separated parameterTSQL 将列中的逗号分隔值与逗号分隔参数进行比较
【发布时间】:2017-09-29 13:15:00
【问题描述】:

在我的查询中,我有一个字段 Keywords,其中包含一个用分号分隔的值列表 ;

在我的 SSRS 报告中,我的参数之一 @Keywords 包含该 Keywords 字段中所有可能的单个值的列表。

如何将多值参数@Keywords 传递给我的查询并检查字段Keywords 是否包含任何选定的参数值?

我有一个拆分函数,可以将@KeywordsKeywords 分隔成它们各自的关键字项,但是如何在我的 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 financecompensation,因此我的报告将显示与示例 Keywords 字段关联的记录。

只要@Keywords 包含Keywords 中的任何值,我们就需要该记录。

【问题讨论】:

    标签: tsql reporting-services compare contains exists


    【解决方案1】:

    you 可能需要将此扩展到您的表选择,我只是有一个概念证明才能使其正常工作,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
    

    让我知道它是否有效。

    【讨论】:

    • 谢谢!我用我自己的 Split 函数替换了您的 ReturnTableofVarchars,结果是两个列表之间共有的两个值,这是一个巨大的帮助。我现在如何在我的WHERE 中评估它?我应该把它放到HAVING COUNT(*) > 1?
    • 是的,有 coutn() 或存在的地方(从上面选择 ..),@gruff 将其标记为已解决或回答,如果这解决了您的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2015-09-05
    • 1970-01-01
    相关资源
    最近更新 更多