• C Hrushi (9/17/2013)


    This might hep for n number of columns :

    *********************************************************************************************

    DECLARE @Q NVARCHAR(4000),

    @CName VARCHAR(255)

    DECLARE ColCur CURSOR

    FOR

    SELECT C.name as Column_name

    FROMsys.columns C

    LEFT JOIN sys.tables t

    on C.object_id = t.object_id

    WHEREt.name = 'Your_table'

    OPEN ColCur

    FETCH NEXT FROM ColCur INTO @CName

    WHILE(@@FETCH_STATUS =0)

    BEGIN

    SET @Q = 'UPDATE Your_Table SET '+@CName+' = NULL WHERE '+@CName+' ISNULL '

    PRINT @Q

    FETCH NEXT FROM ColCur INTO @CName

    END

    CLOSE ColCur

    DEALLOCATE ColCur

    No need for a cursor for something this simple. You can easily use the code I posted. All you need to do is add 'Update MyTable set' + [query results]

    You could use STUFF to build up the comma delimited list of values, the values in this case are the column update sql strings. Then execute the string.

    _______________________________________________________________

    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/