If you are using SQL Server 2005 you don't need to do this in a cursor
anymore.
I talk through an example on my blog:
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/05/11/429.aspx. It's basically using the FOR XML....
create table mailing_list (
individual_name nvarchar(100) not null,
list_name nvarchar(10) not null
)
insert mailing_list ( individual_name, list_name ) values( 'tony r', 'List
A' )
insert mailing_list ( individual_name, list_name ) values( 'tony r', 'List
B' )
insert mailing_list ( individual_name, list_name ) values( 'tony r', 'List
C' )
insert mailing_list ( individual_name, list_name ) values( 'joe r', 'List
A' )
insert mailing_list ( individual_name, list_name ) values( 'joe r', 'List
B' )
insert mailing_list ( individual_name, list_name ) values( 'alex r', 'List
A' )
select distinct
individual_name,
list = substring(
( select ', ' + list_name as [text()]
from mailing_list m2
where m2.individual_name = m1.individual_name
for xml path(''), elements )
, 3, 100 )
from mailing_list m1
Gives this result :-
alex r List A
joe r List A, List B
tony r List A, List B, List C
Tony.
--
Tony Rogerson
SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
Server Consultant
http://sqlserverfaq.com - free video tutorials
[quoted text, click to view] "ybc" <ybc@discussions.microsoft.com> wrote in message
news:4FB91960-0D70-4E37-828D-7106D894BD28@microsoft.com...
> Hi,
> I'd like to know how to append strings to a varible, ie. @var = @var +
> @string
>
> The code is something like this:
>
> while (@@fetch_status = 0)
> begin
> set @message = @message + @user + ';'
> fetch next from user_curs into @user
> end
>
> print @user
>
> It seems string appending doesn't work.
>
> Thanks,
>
> ybc