Using for loop and comma separed values

  • I have a table structure as follows.

    user-|-------------|<- userRole (One user have many roles).

    When I need to insert record to user table I also need to insert user's roles to the userRole table.My idea was to use and procedure in sql and pass the selected user roles id in a comma separated values.Then after insert into user table use id of the new record(getting using SCOPE_IDENTITY()) to insert to the userRole table.

    I wrote the procedure to insert to the user table. But I do not understand how to use the comma separated values to generate records. What I need is

    for(userRole in Comma_User_Roles)

    {

    insert into userRoles();---- the Comma_User_Rolesis input parameter.

    }

    Can anybody help me on this

    Thanks

  • I think what you need is a string splitter

    Please check the article below for reference

    http://www.sqlservercentral.com/articles/Tally+Table/72993/


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

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

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