Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS - Ignore SQL Variables in my query

I have a query that works very well in SQL. When I try to bring this into SSRS, the report asks for 4 parameters. Two of the parameters/variables are actually based on the other Two parameters as such:

DECLARE @Q int       --SET @Q = 1 -- Quarter 
DECLARE @Year int    --SET @Year = 2013

DECLARE @STARTDATE varchar(10)
SELECT @STARTDATE = D FROM (
    select case @Q
        when 1 then '1/1/' + convert(varchar(10),@Year)
        when 2 then '4/1/' + convert(varchar(10),@Year)
        when 3 then '7/1/' + convert(varchar(10),@Year)
        when 4 then '10/1/' + convert(varchar(10),@Year)
    end as D
    ) sd

DECLARE @ENDDATE varchar(10)
SELECT @ENDDATE = D FROM (
    select case @Q
        when 1 then '3/31/' + convert(varchar(10),@Year)
        when 2 then '6/30/' + convert(varchar(10),@Year)
        when 3 then '9/30/' + convert(varchar(10),@Year)
        when 4 then '12/31/' + convert(varchar(10),@Year)
    end as D
    ) ed

--(ADDITIONAL SQL CONTINUES USING ALL 4 PARAMETERS) ...

How can I get SSRS to only ask for the first two parameters (@Q, @Year) and ignore the @StartDate and @EndDate as those are calculated within the query?

like image 371
ajspacemanspiff Avatar asked Apr 09 '13 18:04

ajspacemanspiff


People also ask

How do you make a parameter optional in SSRS?

Set the parameter to 'Allow null value' under General section. Null according to SSRS means it does not have to be set, thus making it optional.

How do I enable and disable parameters in SSRS?

It's not possible to dynamically dis-/enable parameters for reports in Report Manager UI. You have to create an own solution, e.g. using ReportViewer control in an own application / web site.

Can I use variables in SQL query?

You can use variable objects in an SQL query as well as string values.

How do you pass one parameter value to another parameter in SSRS?

To set available values for the second parameter In the Report Data pane, in the Parameters folder, right-click the first parameter, and then click Parameter Properties. In Name, verify that the name of the parameter is correct. Click Available Values. Click Get values from a query.


1 Answers

Parameters are added for the query of an SSRS data set for any variables in the query that aren't also declared in the query.

But once they are added, they aren't always automatically removed, so you may need to manually remove them from your dataset query.

For example, this query will only create parameters for @ParamOne:

DECLARE @StartDate, @EndDate DATETIME

SET @StartDate = 'January 1, 2013'
SET @EndDate = 'February 1, 2013'

SELECT
   UserName,
   Action,
   DateOccurred
FROM
   myTable
WHERE
   DateOccurred BETWEEN @StartDate AND @EndDate
AND UserName = @ParamOne

But SSRS can be picky about capitalization. Make sure they match between your declaration and your uses of the variable.

like image 71
Jamie F Avatar answered Nov 06 '22 05:11

Jamie F