Do I need a nested cursor?

  • Hi,

    I have to get a list of preferences for each customer within a specified group. The Group Id is passed in as a parameter. So,

    You have a group with  multiple customers and each customer has multiple preferences.

    I need to end up with a record set in which each member of the group has had their preferences concatenated together in a single text column. One row per group member.

    Please help.

    Thanks Alot

    Jules

     

    www.sql-library.com[/url]

  • the general convention here is that you should post details of table structures and any attempts you have made, or a specific question related to either.

    it looks a bit like you simply want someone to write your query for you

    cheers

    dbgeezer

  • My post was erased when I tried to post it.

    Anyway, this is something similar that worked for me. One cursor, but not nested.

    declare @client_id int

    declare @client varchar(20)

    declare client_cursor cursor fast_forward for

    select client_id, client_name from tbl_clients

    open client_cursor

    fetch client_cursor into @client_id, @client

    while @@fetch_status=0

    begin

     declare @p varchar(4000)

     set @p=''

     select @p=@p+','+u.name

     from tbl_user_clients uc, tbl_users u

     where uc.client_id = @client_id

     and u.user_id = uc.user_id

     order by uc.user_id

     if @p > '' set @p = substring(@p,2,len(@p)-1)

     select client_name = @client, users = @p

     fetch client_cursor into @client_id, @client

    end

    close client_cursor

    deallocate client_cursor

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

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