• Nice article. You can actually drop the cursor completely and replace with the following

    DECLARE @fields nvarchar(4000)

    DECLARE @values nvarchar(4000)

    SET @fields = ''

    SET @values = ''

    SELECT @fields = @fields+ ',[' + sc.name + ']' ,

    @values = @values+ '+'',''+dbo.'

    + CASEWHEN st.name = 'money' THEN 'spMVal'

    ELSE 'spVal'

    END

    + '([' + sc.name + '])'

    FROM syscolumns sc

    JOIN sysobjects so ON so.id = sc.id

    JOIN systypes st ON st.xusertype = sc.xusertype

    WHERE so.name = @table

    AND st.name <> 'text'

    ORDER BY sc.colorder

    PRINT @fields

    PRINT @values

    In SQL 2000 there is the (mentioned in the caveats) issue with nvarchar(4000) meaning you will potentially run out of string space, this is much less of an issue in 2005 with nvarchar(MAX).

    I hope someone finds this SELECT @STR = @STR + ... useful, I know I have. I have found some occasions where it only returns one line, but if you include TOP 100 percent it will generally solve this issue.