posted 1/26/2010 by MikeDavis - Views: [9451]
When using a stored procedure to call data in SSRS (reporting services) you will need to pass the parameter from the report to the stored procedure. This is easy when you are passing in a single value. When you want to use a multi value parameter you will need to use a split function to divide the multi values into a table. Here is the Table-Valued function I use:
/****** Object: UserDefinedFunction [dbo].[Split] Script Date: 01/26/2010 10:22:51 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[Split]/* This function is used to split up multi-value parameters */(@ItemList NVARCHAR(max),@delimiter CHAR(1))RETURNS @IDTable TABLE (Item NVARCHAR(100) collate database_default )ASBEGINDECLARE @tempItemList NVARCHAR(max)SET @tempItemList = @ItemListDECLARE @i INTDECLARE @Item NVARCHAR(max)SET @tempItemList = REPLACE (@tempItemList, @delimiter + ' ', @delimiter)SET @i = CHARINDEX(@delimiter, @tempItemList)WHILE (LEN(@tempItemList) > 0)BEGINIF @i = 0SET @Item = @tempItemListELSESET @Item = LEFT(@tempItemList, @i - 1)INSERT INTO @IDTable(Item) VALUES(@Item)IF @i = 0SET @tempItemList = ''ELSESET @tempItemList = RIGHT(@tempItemList, LEN(@tempItemList) - @i)SET @i = CHARINDEX(@delimiter, @tempItemList)ENDRETURNEND--use the following line in your stored procedure--WHERE ProductID IN (SELECT Item FROM dbo.Split (@Item, ','))GO
I did not write this function, I cant remember where I got it from so my apologizes to the original developer for not plugging you.
In the query you will need to change the where clause to use the split function.
For Example:
From Production.ProductWHERE Color IN (SELECT Item FROM dbo.Split (@Color, ','))
Notice you have to pass in the comma as the delimiter. This allows the split function to be used in other applications like a pipe delimited string. SSRS is always comma delimited in a multi value parameter.