Iterate through a list in T-SQL

Many is the time that I have had a list, perhaps created in the front-end of a program that, was pushed through to the database and then needed to be iterated through. Essentially I wanted a simple way to do what in JScript or CSharp is a String.Split(','), returning an array of strings.

Usually I just write it into my stored procedure as and when needed, but I a few days ago I took the time to write a function that will do it. There is a section of the script that you can modify to deal with the specifics of your situation (the bit in between the lines of asterisks).

In my code, I’m iterating through the list and building a table of items which my function returns. If you don’t need to return anything just change the function to omit the RETURNS clause.

  1.  
  2. CREATE FUNCTION dbo.ActionItemsFromDelimitedList
  3. (
  4.  @Delimiter varchar(10),
  5.  @List varchar(1000)
  6. )
  7. –omit this if you don't want to return anything or change if you want to return something else
  8. RETURNS @ListTable TABLE
  9. (
  10.  ListItem varchar(50)
  11. )
  12. AS
  13. BEGIN
  14.  DECLARE @pos int
  15.  SET @pos = 0
  16.  DECLARE @ListItem varchar(50)
  17.  SELECT @List=LTRIM(RTRIM(@List))
  18.  –first remove any trailing comma
  19.  WHILE(RIGHT(@List,1)=@Delimiter)
  20.  BEGIN
  21.   SELECT @List=LEFT(@List, LEN(@List)-1)
  22.   SELECT @List=LTRIM(RTRIM(@List))
  23.  END
  24.  
  25.  –start to loop through the list
  26.  WHILE @pos<>LEN(@List)
  27.  BEGIN
  28.   SELECT @pos=CHARINDEX(@Delimiter,@List)
  29.   IF (@pos>0)
  30.    SELECT @ListItem = LTRIM(RTRIM(LEFT(@List, @pos-1)))
  31.   ELSE
  32.    SELECT @ListItem = LTRIM(RTRIM(@List))
  33.  
  34.   –***************************************************
  35.   –start the per item action on ListItem if not blank
  36.   IF @ListItem<>''
  37.    INSERT INTO @ListTable (ListItem) VALUES (@ListItem)
  38.   –end action on ListItem
  39.   –***************************************************
  40.  
  41.   –now set up the list string for the next iteration
  42.   IF (@pos>0)
  43.    SELECT @List=RIGHT(@List, LEN(@List)-@pos)
  44.   ELSE
  45.    SELECT @List=''
  46.  
  47.   SELECT @pos=0
  48.  END
  49.              –omit the return if there is no RETURNS clause at the top of this script
  50.  RETURN
  51. END

I’ve also spent a fair bit of time wondering how I could easily do the opposite. That is, take a table or column of results and convert it into a list. This is easier said than done and there are a number of ways to do it - most of them quite specific to the situation in mind. Check this link out.

I want a simple function (if possible) that will do the job for small recordsets (maximum a few hundred items) and I guess I’d sacrifice raw efficiency for the benefit of having an easy pluggable function that will do it for any recordset. I haven’t been able to do it yet. If and when I crack it, I’ll post the solution.

Comments are closed.

Powered by WordPress

Website design adapted from WordPress theme PrimePress