rogerzebra
Technical User
Hi All,
I'm trying to create a comma delimited list of classcodes and I can't see why the piece of my code beneath doesn't working. The purpose is to add this snippet to a main piece of code and to publish a report on the Reporting Services. I first tried to use a cursor that obviously doesn't work in reporting services? I don't know perhaps that would work but it was way to slow. What I really need is a comma delimit list without a function or if someone could give some good advice on how to make this work with a fake array? So, obviously this is something that I need some advice on, I really would appreciate if someone could help me out here, thanks in advance.
I'm trying to create a comma delimited list of classcodes and I can't see why the piece of my code beneath doesn't working. The purpose is to add this snippet to a main piece of code and to publish a report on the Reporting Services. I first tried to use a cursor that obviously doesn't work in reporting services? I don't know perhaps that would work but it was way to slow. What I really need is a comma delimit list without a function or if someone could give some good advice on how to make this work with a fake array? So, obviously this is something that I need some advice on, I really would appreciate if someone could help me out here, thanks in advance.
Code:
declare @ClassCodeList varchar(1000)
CREATE TABLE #ClassCodeTempList
(
SystemID int
)
DECLARE @SystemID varchar(10), @Pos int
SET @ClassCodeList = LTRIM(RTRIM(@ClassCodeList))+ ','
SET @Pos = CHARINDEX(',', @ClassCodeList, 1)
IF REPLACE(@ClassCodeList, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @SystemID = LTRIM(RTRIM(LEFT(@ClassCodeList, @Pos - 1)))
IF @SystemID <> ''
BEGIN
INSERT INTO #ClassCodeTempList(SystemID) VALUES (CAST(@SystemID AS int))
END
SET @ClassCodeList = RIGHT(@ClassCodeList, LEN(@ClassCodeList) - @Pos)
SET @Pos = CHARINDEX(',',@ClassCodeList, 1)
END
END
SELECT c.SystemID, c.ClassCode
from ClassTbl c
JOIN
#ClassCodeTempList t
ON c.SystemID = t.SystemID
drop table #ClassCodeTempList