Below is code that I built from an example I found online, I can't find the link, but the code is referenced in the answers on this stack overflow question: Passing multiple values for a single parameter in Reporting Services.
Here is the SQL code I am working with right now within my stored procedure, it was a long procedure so I summed it down to just the section I am working on, and added the DECLARE
and SET
for @EMPLOYEES
, which are passed as a parameter from SSRS to make the code snippet run.
DECLARE @EMPLOYEES varchar(8000)
-- EMPLOYEES is a comma separated list of EMPLOYEE IDS
-- FROM SSRS Report Parameters. Each ID is 12 characters
-- And there are 806 Employees to choose from, which
-- when all are selected, the Comma separated string grows
-- to 11,193 characters, much longer than 8000
SET @EMPLOYEES = 'EMP000000001,EMP000000002,EMP000000003'
CREATE TABLE #EMPLOYEEIDS
(
EMPLOYEEID varchar(100) NOT NULL
)
DECLARE @CharIndex AS int
DECLARE @Piece AS varchar(100)
-- FILL THE #EMPLOYEEIDS TABLE WITH THE COMMA SEPARATED EMPLOYEE IDS
SELECT @CharIndex = 1
WHILE @CharIndex > 0 AND LEN(@EMPLOYEES) > 0
BEGIN
SELECT @CharIndex = CHARINDEX(',', @EMPLOYEES)
IF @CharIndex > 0
SELECT @Piece = LEFT(@EMPLOYEES, @CharIndex - 1)
ELSE
SELECT @Piece = @EMPLOYEES
INSERT INTO #EMPLOYEEIDS (EMPLOYEEID) VALUES (@Piece)
SELECT @EMPLOYEES = RIGHT(@EMPLOYEES, LEN(@EMPLOYEES) - @CharIndex)
END
SELECT * FROM #EMPLOYEEIDS
DROP TABLE #EMPLOYEEIDS
I had 6 sets of multi-values, all of them worked fine, until I found that the reports were missing much of the data for employees, to which I found that the VARCHAR(8000)
was overflowed when selecting all the employees on the report parameters (there are over 800 of them). The Report would run, SQL would happily truncate the VARCHAR
to 8000 characters, and a quarter of the IDS were not parsed.
So I tried to switch the VARCHAR
to a text field, and none of the parsing functions would work when the field is set up as TEXT. I get errors like the following:
Msg 8116, Level 16, State 2, Procedure usp_QualityMonitoring_AllProfiles_SelectWithParameters, Line 89
Argument data type text is invalid for argument 1 of left function.
This is understandable, I know that many functions that work with VARCHAR
will not work with TEXT
. So, SQL is truncating everything after 8000 characters when I use a VARCHAR
, and the procedure won't ever run if I switch it to TEXT
.
- What other options to I have to pass multi-valued parameters from SSRS to a SQL Server stored procedure that can support this many options?
- OR is there a way to fix the code in the stored procedure to parse through
TEXT
instead ofVARCHAR
?
Note: I originally thought the SQL Server running the Stored Proc was 2005, but I have determined that it is not:
SELECT @@VERSION
-- Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) May 3 2005 23:18:38 Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)