After working on a different question here on SO, I stumbled across recursive CTEs which would on the surface seem a fairly easy way to solve the "Split a csv to table rows" problem.
I put this example together
DECLARE @InputString varchar(255) = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'
SELECT @InputString = @InputString + ','
;
with MyCTE(x,y)
as
(
SELECT
x = SUBSTRING(@InputString,0,PATINDEX('%,%',@InputString)),
y = SUBSTRING(@InputString,PATINDEX('%,%',@InputString)+1,LEN(@InputString))
UNION ALL
SELECT
x = SUBSTRING(y,0,PATINDEX('%,%',y)),
y = SUBSTRING(y,PATINDEX('%,%',y)+1,LEN(y))
FROM
MyCTE
WHERE
SUBSTRING(y,PATINDEX('%,%',y)+1,LEN(y)) <> '' OR
SUBSTRING(y,0,PATINDEX('%,%',y)) <> ''
)
SELECT x FROM MyCTE
OPTION (MAXRECURSION 2000);
GO
Is this really a bad idea? What is the overhead in SQL for recursive queries like this, and what are the potential pitfalls for this kind of approach.
Incidentally, I'm thinking this idea/technique could probably be leveraged to solve this other question.