简体   繁体   English

将SSRS参数传递给SQL查询

[英]Pass SSRS parameter to SQL query

I've built an SSRS report that is supposed to look at data from one of several tables with similar names - ie, table001, table010, table011, etc. When I was building the report, I was only including three of the tables, out of more than a dozen. 我已经构建了一个SSRS报告,该报告应该查看来自具有相似名称的几个表之一的数据 - 即table001,table010,table011等。当我构建报告时,我只包括其中的三个表,十几个。 Everything worked fine until I added all the rest of the tables to the query, using multiple SELECT statements UNIONed together; 一切正常,直到我将所有其余表添加到查询中,使用多个SELECT语句UNIONed; it was trying to sort through so much data that it was taking almost half an hour to render the report. 它试图对如此多的数据进行排序,以便花费近半个小时来呈现报告。 That's not going to work. 那不行。

Is there a way to pass an SSRS parameter to the SQL query, specifying which table to access? 有没有办法将SSRS参数传递给SQL查询,指定要访问的表?

Based on your comments, you could do the following 根据您的评论,您可以执行以下操作

DECLARE @Parameter NVARCHAR(15) = 'Foo'

SELECT CASE 
WHEN @Parameter IN ('Foo', 'Bar')
    THEN (
            SELECT *
            FROM table001
         ) 
WHEN @Parameter IN ('Foobar')
    THEN (
            SELECT *
            FROM table002
         ) 
ELSE
         (
            SELECT *
            FROM table003
         )
END


Alternately 交替

SELECT CASE @Parameter 
WHEN 'Foo'
    THEN (
            SELECT *
            FROM table001
         ) 
WHEN 'Bar'
    THEN (
            SELECT *
            FROM table002
         ) 
WHEN 'Foobar'
    THEN (
            SELECT *
            FROM table003
         ) 
ELSE
    (
        SELECT *
        FROM @table004
    )
END

Have you followed the MSDN tutorial? 你有没有关注过MSDN教程? This is good too: http://sql-bi-dev.blogspot.com/2010/07/report-parameters-in-ssrs-2008.html 这也很好: http//sql-bi-dev.blogspot.com/2010/07/report-parameters-in-ssrs-2008.html

Please share what you have tried and where you are having trouble. 请分享您尝试过的内容以及遇到问题的地方。 Essentially, you define a parameter in the report and include it in your query. 实质上,您在报表中定义参数并将其包含在查询中。 SSRS provides the parameter value (or it is received through user input), and then the final query is passed off to the database. SSRS提供参数值(或通过用户输入接收),然后将最终查询传递给数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM