Help With sp and Comma Delimited String

  • Roles

    Admin

    User

    DeleteAll

    ReadAll

    Report

    The above "Roles" table contains a list of values. I have a stored procedure that must return a table with one field called retVal that is equal to a comma delimited list of all the values found in the above "Roles" table. Basically, I need it to return a table looking like this

    retVal='Admin,User,DeleteAll,ReadAll,Report'

    Is there an easy way to do this with SQL? I can use CURSORS and do row by row processing, but it seems like there should be an easier way. Does anyone know how to do this easily without CURSORS?

  • This is one way to go about it....

    Declare @RetVal Varchar(1000)

    SET @RetVal = ''

    SELECT @RetVal = @RetVal+','+(Select Min(RoleName) from Roles where Roles.RoleName >= Outer1.RoleName) from Roles Outer1

    SELECT Substring(@RetVal,2,Len(@RetVal))

    -- eliminate first ','

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply