select D.DepartmentID, D.Name as DepartmentName
from HumanResources.Department D
order by D.Name asc
CREATE PROCEDURE MultiValueParam
@MyParam varchar(1000)
AS
BEGIN
SELECT @MyParam as TheParameterReturned;
END
ALTER PROCEDURE GetEmployeeData
@HireDate date,
@DepartmentList varchar(1000)
AS
BEGIN
SELECT E.NationalIDNumber, E.JobTitle, E.BirthDate, E.MaritalStatus, E.Gender,
E.HireDate, E.SalariedFlag, E.VacationHours, E.SickLeaveHours,
D.GroupName as DepartmentGroupName, D.Name as DepartmentName,
P.FirstName, P.MiddleName, P.LastName
FROM HumanResources.Employee E
INNER JOIN HumanResources.EmployeeDepartmentHistory EDH
ON EDH.BusinessEntityID = E.BusinessEntityID
AND EDH.EndDate IS NULL -- current active department does not have EndDate filled in
INNER JOIN HumanResources.Department D
ON D.DepartmentID = EDH.DepartmentID
INNER JOIN Person.Person P
ON P.BusinessEntityID = E.BusinessEntityID
WHERE E.HireDate > @HireDate
AND D.DepartmentID IN (@DepartmentList);
END
Conversion failed when converting the varchar value '12,1,16,14,10,9,11,4,7,8,5,13,6,3,1 5,2' to data type smallint.
SELECT E.NationalIDNumber, E.JobTitle, E.BirthDate, E.MaritalStatus, E.Gender,
E.HireDate, E.SalariedFlag, E.VacationHours, E.SickLeaveHours,
D.GroupName as DepartmentGroupName, D.Name as DepartmentName,
P.FirstName, P.MiddleName, P.LastName
FROM HumanResources.Employee E
INNER JOIN HumanResources.EmployeeDepartmentHistory EDH
ON EDH.BusinessEntityID = E.BusinessEntityID
AND EDH.EndDate IS NULL -- current active department does not have EndDate filled in
INNER JOIN HumanResources.Department D
ON D.DepartmentID = EDH.DepartmentID
INNER JOIN Person.Person P
ON P.BusinessEntityID = E.BusinessEntityID
WHERE D.DepartmentID IN (12,1,16,14,10,9,11,4,7,8,5,13,6,3,15,2);
SELECT E.NationalIDNumber, E.JobTitle, E.BirthDate, E.MaritalStatus, E.Gender,
E.HireDate, E.SalariedFlag, E.VacationHours, E.SickLeaveHours,
D.GroupName as DepartmentGroupName, D.Name as DepartmentName,
P.FirstName, P.MiddleName, P.LastName
FROM HumanResources.Employee E
INNER JOIN HumanResources.EmployeeDepartmentHistory EDH
ON EDH.BusinessEntityID = E.BusinessEntityID
AND EDH.EndDate IS NULL -- current active department does not have EndDate filled in
INNER JOIN HumanResources.Department D
ON D.DepartmentID = EDH.DepartmentID
INNER JOIN Person.Person P
ON P.BusinessEntityID = E.BusinessEntityID
WHERE D.DepartmentID IN ('12,1,16,14,10,9,11,4,7,8,5,13,6,3,15,2');
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '12,1,16,14,10,9,11,4,7,8,5,13,6,3,1 5,2' to data type smallint.
WHERE D.DepartmentID IN ('12');
-- from http://www.sommarskog.se/arrays-in-sql-2005.html
-- original name: iter$simple_intlist_to_tbl
CREATE FUNCTION list_to_tbl (@list nvarchar(MAX))
RETURNS @tbl TABLE (number int NOT NULL) AS
BEGIN
DECLARE @pos int,
@nextpos int,
@valuelen int
SELECT @pos = 0, @nextpos = 1
WHILE @nextpos > 0
BEGIN
SELECT @nextpos = charindex(',', @list, @pos + 1)
SELECT @valuelen = CASE WHEN @nextpos > 0
THEN @nextpos
ELSE len(@list) + 1
END - @pos - 1
INSERT @tbl (number)
VALUES (convert(int, substring(@list, @pos + 1, @valuelen)))
SELECT @pos = @nextpos
END
RETURN
END
ALTER PROCEDURE GetEmployeeData
@HireDate date,
@DepartmentList varchar(1000)
AS
BEGIN
SELECT E.NationalIDNumber, E.JobTitle, E.BirthDate, E.MaritalStatus, E.Gender,
E.HireDate, E.SalariedFlag, E.VacationHours, E.SickLeaveHours,
D.GroupName as DepartmentGroupName, D.Name as DepartmentName,
P.FirstName, P.MiddleName, P.LastName
FROM HumanResources.Employee E
INNER JOIN HumanResources.EmployeeDepartmentHistory EDH
ON EDH.BusinessEntityID = E.BusinessEntityID
AND EDH.EndDate IS NULL -- current active department does not have EndDate filled in
INNER JOIN HumanResources.Department D
ON D.DepartmentID = EDH.DepartmentID
INNER JOIN Person.Person P
ON P.BusinessEntityID = E.BusinessEntityID
WHERE E.HireDate > @HireDate
AND D.DepartmentID IN (select * from list_to_tbl(@DepartmentList));
END
=Join(Parameters!DepartmentList.Label, ", ")
=Join(Parameters!DepartmentList.Label, vbCrLf)
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (8)
Commented:
I Think you can Improve this by modifying the =join() with changing the delimiter to custom delimeter and passing to the stored procedure.
This is needed because suppose the values in the multi value parameter contains like 'Raj, Aryan,Ravi, Kumar' but here the I need it to be like 'Raj, Aryan!@#$$Ravi, Kumar' here delimeter sequence is !@#$$ will be helpful if we have the comma in the parameter values.
Then In the List_to_Tbl Function You have to modify the Delimeter to be only ',' use the some sequence like the Example I given.
Author
Commented:Working link: Delimited list as parameter: what are the options?
Commented:
Author
Commented:Commented:
Is there anyway the 'select all' functionality can be added into the stored procedure?
I'm referring to the article: http://blog.hoegaerden.be/2013/12/18/the-select-all-parameter-option-ssrs/
I have added an, OR statement at the end of the WHERE clause as follows:
WHERE (E.HireDate > @HireDate
AND D.DepartmentID IN (select * from list_to_tbl(@DepartmentLis
But produces the following error:
Conversion failed when converting the varchar value '-1,1' to data type int.
Any suggestions welcome. Thank you
View More