【问题标题】:Dynamic sql query in SSRS (Sql Server Report Server 2012)SSRS 中的动态 sql 查询(Sql Server Report Server 2012)
【发布时间】:2013-01-16 18:28:06
【问题描述】:

我在 vs2010 中创建了一个名为“clients”的报告,其中显示了具有以下属性的客户端列表:

clientID,firstname,lastname,adres,country,birthday

我已使用数据源和数据集创建了此报告。在这个数据集中,我创建了一个这样的查询:

select firstname, lastname, adres, country, birthday
from clients

这行得通!

我要加2个optonal parameters

param_clientID ,param_birthDay

我想在 where 子句中使用这些参数,前提是它们已被填充。

where clientID = param_clientID and birthday = param_birthDay

应该有可能填写了clientID,而没有填写生日参数。否则也一样。

我该怎么做?

【问题讨论】:

    标签: sql dynamic reporting-services


    【解决方案1】:

    向您的报告添加(可选)参数非常容易。

    首先让你的参数可以为空。

    select firstname, lastname, adres, country, birthday 
    from clients
    where (clientID = @clientID or @clientID is null) 
    and (birthday = @birthDay or @birthDay is null)
    

    更详细的说明:

    【讨论】:

    • 原始发帖人确实要求仅在使用参数时包含 where 子句。 using 或 null 可能会导致严重的性能问题