Search code examples
variablesreporting-servicesparameters

Parameter-dependent variables in SSRS report query


My SSRS report has 2 input parameters @myParm1 and @myParm2. The report's main query requires 2 variables @myVar1 and @myVar2, both of which have values determined by @myParm2. The simplified code shown below executes properly via SSMS. When I run it via SSRS (without the SET statements and with appropriate query-driven datasets enabling user selection of @myParm1 and @myParm2) I get a blank report without any error messages. SSRS allows variables which can be set up via the Report Properties dialog, but I haven't been able to configure SSRS variables that work in my main query. Should I be using SSRS variables or is a different approach needed?

DECLARE @myParm1    char(1) -- input parameter
DECLARE @myParm2    char(1) -- input parameter

DECLARE @myVar1 int -- variable dependent upon @myParm2
DECLARE @myVar2 int -- variable dependent upon @myParm2

SET @myParm1 = 'T'

SET @myParm2 = 'A'

IF  @myParm2 = 'A'
    BEGIN
        SET @myVar1 = 1
        SET @myVar2 = NULL
    END
ELSE IF @myParm2 = 'B'
    BEGIN
        SET @myVar1 = 2
        SET @myVar2 = 9670
    END
ELSE IF @myParm2 = 'C'
    BEGIN
        SET @myVar1 = 3
        SET @myVar2 = NULL
    END
ELSE IF @myParm2 = 'D'
    BEGIN
        SET @myVar1 = 4
        SET @myVar2 = NULL
    END

select      

    @myVar1 as 'FirstEventType',

    @myVar2 as 'SecondEventType',

    (
    select      t3.Description
    from        myTable2 t2
    inner join  myTable3 t3 on t2.keyColumn = t3.keyColumn
    where       t2.Type = @myParm2
    and     t2.keyColumn = t1.keyColumn
    ) as 'TypeDescription',

    (
        select  t2.DateColumn
        from    myTable2 t2
        where   t2.Type = @myParm2
        and t2.keyColumn = t1.keyColumn
    ) as 'Date'
                
from    myTable1 t1
where   t1.PropertyType = @myParm1```

Solution

  • Create the two parameters and remove any DECLARE and SET of @myParm1 and @myParm2 inside the dataset (commented out in my example)

    enter image description here

    A simplified version of your query showing that parameters work

    -- DECLARE @myParm1 CHAR(1) -- input parameter
    -- DECLARE @myParm2 CHAR(1) -- input parameter
    DECLARE @myVar1 INT -- variable dependent upon @myParm2
    DECLARE @myVar2 INT -- variable dependent upon @myParm2
    
    -- SET @myParm1 = 'T'
    -- SET @myParm2 = 'A'
    
    IF @myParm2 = 'A'
    BEGIN
        SET @myVar1 = 1
        SET @myVar2 = NULL
    END
    ELSE IF @myParm2 = 'B'
    BEGIN
        SET @myVar1 = 2
        SET @myVar2 = 9670
    END
    ELSE IF @myParm2 = 'C'
    BEGIN
        SET @myVar1 = 3
        SET @myVar2 = NULL
    END
    ELSE IF @myParm2 = 'D'
    BEGIN
        SET @myVar1 = 4
        SET @myVar2 = NULL
    END
    
    SELECT @myParm1 AS myParm1,
        @myParm2 AS myParm2,
        @myVar1 AS myVar1,
        @myVar2 AS myVar2
    

    To display NULL string when value is null you can use an expression like

    =Iif(Fields!myParm2.value  IS Nothing, "NULL",Fields!myParm2.value)
    

    enter image description here