Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
lw1990
Yak Posting Veteran
85 Posts |
Posted - 2008-10-13 : 18:16:26
|
Hi,Please help me to create a query that I'll run in the SQL Query Analyzer to process a table.The table has 3 fields(A, B, C):A--- B-------- CJohn JohnDoc1 NULLMatt MattDoc1 NULLStan StanDoc1 NULLMatt MattDoc2 NULLMatt MattDoc3 NULLJohn JohnDoc2 NULL.......What I want to do is to write in Field C with the value of:If the name was repeated in field A then expend the Docname in B and put in field C. If not repeated, then keek the field C Null. So, after execute the S.P., the table should be like this:A--- B-------- CJohn JohnDoc1 JohnDoc1;JohnDoc2Matt MattDoc1 MattDoc1;MattDoc2;MattDoc3Stan StanDoc1 NULLMatt MattDoc2 MattDoc1;MattDoc2;MattDoc3Matt MattDoc3 MattDoc1;MattDoc2;MattDoc3John JohnDoc2 JohnDoc1;JohnDoc2......The table has more than 15000 records.Please help me with any idea.Thanks. |
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-13 : 18:46:02
|
declare @c varchar(8000), @a varchar(10) declare cur cursor FOR select distinct a from #tab1 open cur fetch next from cur into @a while @@fetch_status = 0begin set @c = 'update #tab1 set c = replace(ltrim(''' select @c = @c+' '+b from #tab1 where a = @a select @c = @c + '''),'' '','';'') from #tab1 where a = '''+@a+''''execute(@c)fetch next from cur into @a endclose curdeallocate cur |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 01:31:49
|
see here alsohttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53293 |
 |
|
lw1990
Yak Posting Veteran
85 Posts |
Posted - 2008-10-14 : 11:38:02
|
Thank you very much for the code and the link.I get the table updated with the result I wanted. |
 |
|
|
|
|