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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to convert Multiple Records in to CSV

Author  Topic 

praveen050
Starting Member

9 Posts

Posted - 2012-05-23 : 03:00:40
Write a SQL to convert Multiple Records in to Comma Separated values and Insert into Different table
Write a sql to load data as comma separated one record per Agent in to T_AGENT_CSVEXT.
EX: if TAGENT has
Agent_ID Agent_Extension
aapena 83601014
aapena 83601014
aapena 83605027
aapena 83605236
aapena 83605473
aapena 83605724

T_AGENT_CSVEXT should have
Agent_ID Agent_Extension
aapena 83601014,83605027,83605236,83605473,83605724

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-23 : 03:12:11
Thank you for posting your assignment question.

Can you also shared with us the solution ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

praveen050
Starting Member

9 Posts

Posted - 2012-05-23 : 03:14:14
i m not getting the solution
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-23 : 03:57:03
what have you tried ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

praveen050
Starting Member

9 Posts

Posted - 2012-05-23 : 09:04:02
open tabcur
declare @g varchar(1000)
fetch tabcur into @g
while (@@FETCH_STATUS=0)
begin
if (@g=@g)

DECLARE @ext varchar(100)
SELECT @ext = COALESCE(@ext + ', ', '') + cast(id as varchar) FROM tab where mm=@g
print @g +' ' +@ext

fetch tabcur into @g
end

and my result

hh 1, 2, 3, 4
hh 1, 2, 3, 4, 1, 2, 3, 4
hh 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4
hh 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4
gg 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7
gg 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 5, 6, 7
gg 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 5, 6, 7, 5, 6, 7

but i want
hh 1,2,3,4
gg 5,6,7
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-23 : 09:24:22
refer to this thread http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254
and use the for xml path method shown there


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

praveen050
Starting Member

9 Posts

Posted - 2012-05-24 : 02:14:14
Thanks for your suggestion i got the answer, actually i have table with more than 8000 rows
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-24 : 02:47:02
quote:
Originally posted by praveen050

Thanks for your suggestion i got the answer, actually i have table with more than 8000 rows


that will not be an issue.

Just use the SELECT query in that thread


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -