0

I'm using SQL Server Reporting Services to make a few reports. I have a few reports where filters are optional.

For example .. Imagine you have a report of books. The end user has the option to specify (or filter by) none, one, or many authors.

I do not believe I can put an IF { } statement into my query?

If there a suggest or best way to do this in SQL Server Reporting Services? thanks!

5
  • There are quite many ways to solve this matter but you really should provide some actual example. Commented Apr 12, 2011 at 21:17
  • Why can't you put an IF statement into your query? Commented Apr 12, 2011 at 21:18
  • Duplicate of stackoverflow.com/q/5187649/243925 ?? Commented Apr 12, 2011 at 21:30
  • Tony - you are right it is a duplicate. However I tagged this in the Reporting Services as there may be a built in way to handle it. Commented Apr 13, 2011 at 15:23
  • Updated my answer as I didn't realise it was for SSRS until the tag was changed... same answer stillapplies, you just need to do something in SSRS to use it... see my answer Commented Apr 13, 2011 at 21:14

3 Answers 3

4

You can do this anywhere you want a condition..

WHERE ((:param0 IS NULL) OR (column_name0 = :value0)) 
  AND ((:value1 IS NULL) OR (column_name1 = :value1)) 
  AND...

Pass Null into the parameter if you don't want to use it.

In SSRS, you'll need to set the paremeters as NULL by default in SSRS... see here for more detail

Sign up to request clarification or add additional context in comments.

2 Comments

"Pass Null into the parameter if you don't want to use it" -- can also create the procedure with the parameters defaulting to NULL. Can also use a 'magic' default value other than NULL ;)
@onedaywhen: +1, yup that too :) - though be careful, you could accidentally get all rows returned :/
3

Its hard to tell without seeing the whole procedure that you've written, but you can have multiple values in your WHERE clause by using the statement IN,

eg:

SELECT
    *
FROM
   BOOKS 
WHERE
   AUTHOR IN ('AUTHOR1', 'AUTHOR2', ...)

You can provide everything in the author list in a single (type eg VARCHAR(MAX), TEXT) parameter if you like by using a set delimiter and then using a split function in your WHERE clause. Obviously if this parameter is empty or NULL the procedure would return all books.

There may be other approaches but this one works for me.

Comments

0

One solution is to push the requested authors into a staging table and query from that.

Select ...
From books
Where Exists    (   
                Select 1
                From author_staging As S
                Where S.author = books.author
                )

If the desired behavior was to return all books if no authors were selected, then you could do:

Select ...
From books
Where Exists    (   
                Select 1
                From author_staging As S
                Where S.author = books.author
                Union All
                Select 1
                From ( Select 1 As Value ) As Z
                Where Not Exists    (
                                    Select 1
                                    From author_staging As S1
                                    )
                )

Or another form:

Select ...
From books
Where Exists    (   
                Select 1
                From author_staging As S
                Where S.author = books.author
                )
    Or Not Exists   (
                    Select 1
                    From author_staging As S1
                    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.