Showing posts with label SQLServer. Show all posts
Showing posts with label SQLServer. Show all posts

Friday, January 31, 2014

SQL Server: use a cursor to fetch rows, then split a comma separated string in a column, and loop all the values

Some time ago I wrote a post about "How to split a comma separated string and loop it's values in SQL Server".
Today an anonymous user asked how does this apply if you are reading comma separated string from a column in a DB. 

The simplest way would probably be to do these kind of thing in a proper programming language (C++, C#, VB, Java.... you name it)
But if you really need to do this in T-SQL you can simply use a SQL Server cursor to enumerate the rows of the table, and then split the string one-by-one.

So, suppose we have a column in a table that contain comma separated values, we ca use a stored procedure with the following code. (as of today this code is UNTESTED, just because I don't have a SQL Server instance installed on my home computer :-) )
DECLARE @valueList varchar(8000) 

DECLARE @pos INT
DECLARE @len INT
DECLARE @value varchar(8000) 

DECLARE my_string_table_cursor CURSOR
    FOR SELECT my_column_string FROM my_table

OPEN my_string_table_cursor 


    FETCH NEXT FROM my_string_table_cursor
    INTO @valueList 

    WHILE @@FETCH_STATUS = 0
    BEGIN

        set @pos = 0
        set @len = 0

        WHILE CHARINDEX(',', @valueList, @pos+1)>0
        BEGIN
            set @len = CHARINDEX(',', @valueList, @pos+1) - @pos
            set @value = SUBSTRING(@valueList, @pos, @len)
            --SELECT @pos, @len, @value /*this is here for debugging*/
                
            PRINT @value
            --Here is you value
            --DO YOUR STUFF HERE
            --DO YOUR STUFF HERE
            --DO YOUR STUFF HERE
            --DO YOUR STUFF HERE
            --DO YOUR STUFF HERE

            set @pos = CHARINDEX(',', @valueList, @pos+@len) +1
        END 

        FETCH NEXT FROM my_string_table_cursor 
        INTO @valueList  

    END

CLOSE my_string_table_cursor
DEALLOCATE my_string_table_cursor
The table should contain a column with values like
    row 1:  aa,bb,cc,dwefwef,43truygtfuye,w,
    row 2:  trd , ygu , umhb , bhu,
    row 3:  a,b,c,d,e,f,g,h,i,
    row 4:  123,a,456,bbb,kkk,00000,
And the string in each row must end with a comma ","

SQL Code colored with this syntax highlighter

Thursday, March 7, 2013

How to split a comma separated string and loop it's values in SQL Server

Here is a simple T-SQL script for SQL Server that will split a comma separated string and loop on the values.
It's a simple way to create a array/list of things in SQL Server, and then do something on the values of the list.
These values can be anything: table names, stored procedures, query...

Application note:  the value list string must end with a comma ","
I've update the code to work even with a value list that doesn't have a trailing comma

DECLARE @valueList varchar(8000)
DECLARE @pos INT
DECLARE @len INT
DECLARE @value varchar(8000)

SET @valueList = 'aa,bb,cc,f,sduygfdctys,w,e,r,t,sd sdf sdf,yyy yyy yy,'

--the value list string must end with a comma ','
--so, if the last comma it's not there, the following IF will add a trailing comma to the value list
IF @valueList NOT LIKE '%,'
BEGIN
    set @valueList = @valueList + ','
END


set @pos = 0
set @len = 0

WHILE CHARINDEX(',', @valueList, @pos+1)>0
BEGIN
    set @len = CHARINDEX(',', @valueList, @pos+1) - @pos
    set @value = SUBSTRING(@valueList, @pos, @len)
    --SELECT @pos, @len, @value /*this is here for debugging*/
        
    PRINT @value
    --Here is you value
    --DO YOUR STUFF HERE
    --DO YOUR STUFF HERE
    --DO YOUR STUFF HERE
    --DO YOUR STUFF HERE
    --DO YOUR STUFF HERE

    set @pos = CHARINDEX(',', @valueList, @pos+@len) +1
END


The output of this script is:

    aa
    bb
    cc
    f
    sduygfdctys
    w
    e
    r
    t
    sd sdf sdf
    yyy yyy yy


SQL Code colored with this syntax highlighter

How to rebuild all index on all tables in a SQL Server database

This code will rebuild all indexes on all tables of the current SQL Server DB (without changing the fill factor)
 
Here is the T-SQL code:
DECLARE @tbName varchar(255)

DECLARE tbCursor CURSOR FOR
    SELECT table_name FROM information_schema.tables
    WHERE table_type = 'base table'

OPEN tbCursor

FETCH NEXT FROM tbCursor INTO @tbName
    WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT 'rebuilding all indexes on ' + @tbName
        DBCC DBREINDEX (@tbName,' ',0) WITH NO_INFOMSGS 
        FETCH NEXT FROM tbCursor INTO @tbName
    END
CLOSE tbCursor

DEALLOCATE tbCursor

The source for this code is in the comments of this blog post
I just changed the code a bit, and color-coded it for easier reading  (using this highlighter)