Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 how efficient is this CSV parse SP ???

Author  Topic 

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2005-03-31 : 04:04:03
hello,
i have this code below to parse a CSV string.
it uses the tempdb.

I would like to criticisms to know how efficient this code is ?

thanks

Code:

use tempdb
Go

create procedure ProcessArray (
@InArray as Varchar(8000), @Delimter Char(1)
)
AS

--declarations
declare @Element Varchar(8000)
declare @continue int
declare @cPos int

--
set @continue = 1

--add a trailing delimiter to the array
select @InArray = @InArray + ','

--create a temporary table to hold each element
create table #tmpArray (idx smallint identity(0,1) Primary Key, elementValue varchar(8000))

WHILE (@continue >= 1)
BEGIN
select @cPos = CHARINDEX(@Delimter, @InArray)
select @Element = Rtrim(Ltrim(SUBSTRING(@InArray,1, @cPos -1)))

BEGIN
INSERT #tmpArray (elementValue) VALUES (@Element)
END

select @InArray = SUBSTRING(@InArray, @cPos + 1, DATALENGTH(@InArray))
select @continue = CHARINDEX(@Delimter, @InArray)
END

--Return the element values, we could do some more work on the values here
select * from #tmpArray
drop table #tmpArray
GO

--Run the stored procedure
Declare @RC int
EXEC @RC = ProcessArray @InArray = 'george, fred, mary, ehioze, lekan, Jay, moni, hello, afrika', @Delimter = ','
Go

--get rid of the clutter, it was just an example
drop procedure ProcessArray
GO
   

- Advertisement -