June 22, 2011 at 6:58 am
I have a report that allows multi-select on a company list. When this string is passed to the SP I am using the function below to parse through the string. I do not like this way because it causes too many table scans and this parser is used on a few more fields and just not company. Does anyone know of a way I could do this differently. Thank you very much
RETURNS @Values TABLE ([Param] nvarchar(4000))
AS
BEGIN
DECLARE @chrind INT
DECLARE @Piece nvarchar(4000)
SELECT @chrind = 1
WHILE @chrind > 0
BEGIN
SELECT @chrind = CHARINDEX(@Delim,@RepParam)
IF @chrind > 0
BEGIN
SELECT @Piece = LEFT(@RepParam,@chrind - 1)
END
ELSE
BEGIN
SELECT @Piece = @RepParam
END
IF (@IsString = 1)
BEGIN
INSERT @Values(Param) VALUES(@Piece)
END
ELSE
BEGIN
INSERT @Values(Param) VALUES(CONVERT(INT, @Piece))
END
SELECT @RepParam = RIGHT(@RepParam,LEN(@RepParam) - @chrind)
IF LEN(@RepParam) = 0 BREAK
END
RETURN
END
June 22, 2011 at 7:40 am
This was removed by the editor as SPAM
June 22, 2011 at 7:44 am
For even better performance read Jeff Moden's Tally Oh![/url] article.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
June 22, 2011 at 7:57 am
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply