T-SQL + SSRS parameter problem

Status
Not open for further replies.

StanGable

Ars Legatus Legionis
18,817
I have a parameter in a SQL Server Reporting Services report, @Status, that can have one or more values:

Code:
@Status = Open
@Status = Closed
@Status = Neutral

SSRS then of course passes it into my embedded query as ('Open'), or ('Open', 'Neutral'), etc, and I can filter on it in the WHERE clause:

Code:
SELECT *
FROM table1
WHERE fieldStatus IN (@Status)

However, now I need to also filter based on a date field if "Open" is the *only* status chosen. Stated differently, if the user is only viewing the Open records, they only want records after a certain date. Loosely, the logic goes like this:

Code:
SELECT *
FROM table1
WHERE fieldStatus IN (@Status)
AND 
(CASE WHEN @Status = 'Open' AND COUNT(@Status) = 1 THEN fieldDate >= GETDATE() END)

I hope that makes sense.

Anyone know how to test for a multi-value parameter in that manner? I've come up with solutions that work as long as only a single value is chosen for @Status (I'll post them in a minute), but they fail when @Status comes in with two or more values chosen.

Thanks in advance.
 

A.W.

Ars Tribunus Angusticlavius
6,790
Unfortunately, IN doesn't work with comma separated values. Edit to clarify: It doesn't work with comma separated values in a single parameter.

I normally use a table-valued function to turn the comma separated values into a table, and use:

Code:
IN (select value from dbo.udfCSVToTable(@Status)).

Edit: here's the actual function I use, which I got from the Internets somewhere, and modified slightly. It's set up to return a table with one column of type int, but you could change that to any data type you might want.

Code:
/****** Object:  UserDefinedFunction [dbo].[fnCSVToIntTable]    Script Date: 03/22/2012 19:44:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[fnCSVToIntTable] ( @StringInput VARCHAR(8000) )
RETURNS @OutputTable TABLE ( value int )
AS
BEGIN
	if @StringInput is null or @StringInput = ''
		return
		
    DECLARE @String    VARCHAR(10)

    WHILE LEN(@StringInput) > 0
    BEGIN
        SET @String      = LEFT(@StringInput, 
                                ISNULL(NULLIF(CHARINDEX(',', @StringInput) - 1, -1),
                                LEN(@StringInput)))
        SET @StringInput = SUBSTRING(@StringInput,
                                     ISNULL(NULLIF(CHARINDEX(',', @StringInput), 0),
                                     LEN(@StringInput)) + 1, LEN(@StringInput))

        INSERT INTO @OutputTable ( value )
        VALUES ( cast(@String as int) )
    END
    
    RETURN
END
 

StanGable

Ars Legatus Legionis
18,817
Thanks, AW. I'll look that over- it's a hassle to get the OK to add objects to the database, though, so I may not go there if I don't have to.

However, I suspect I could skip doing this in SQL and just apply a filter to the tablix in SSRS. Problem is I'm fairly new to SSRS and can't quite get the hang of how to do it, so if anyone has any advice, I'd appreciate it.

Thanks again, AW.
 

StanGable

Ars Legatus Legionis
18,817
A.W.":1e78i4qu said:
Doing it in pure SQL would likely be faster. This may not matter if the data set is small, but for large data sets (more than a couple of million rows), you'll probably want to do the filtering before the data gets to SSRS.

Yeah, I knew there would be a performance hit, but in this case (as you allude to) it's pretty much only theoretical due to the relatively small datasets.

Also, since I can't figure out a way to do it in SQL without the rigmarole of getting them to create my function on the prod servers, this will have to do.
 

StanGable

Ars Legatus Legionis
18,817
A.W.":iy5jn159 said:
You could in-line the function in the sproc. Just make a table variable and put the contents of the parameter into it, then select from it in your IN clase.

I hadn't thought of that- great suggestion. I've already sent the report to production, but if there is any more work to be done on it, I'll implement your strategy.

Thanks again, A.W.
 
Status
Not open for further replies.