SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Want to add Text data


Want to add Text data

Author
Message
niladri.primalink
niladri.primalink
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 226
Hi All,

I have table called T1 where the data is populated like below

LineNumber errortext Fileid
1 a 10
1 b 10
2 a 11
2 b 11
2 c 11
3 c 12
3 d 12

Now I have a table called t2 with same structure. Now I want to insert the records into t2 table and the data should be coming from T1 table. where the id is same it will take one id and one linenumber and concatenate of errortext field. My output will be like below

LineNumber errortext Fileid
1 a,b 10
2 a,b,c 11
3 c,d 12

File id should be taken once and then distinct of linenumber field and then concatenate of errortext field.

Thanks in advance for your help!!
Mark Fitzgerald-331224
Mark Fitzgerald-331224
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3886 Visits: 1538
Quick and dirty solution which would not scale that well but works as below. You may want to investigate
CLR concatenate aggregate


declare @a table (Linenumber int, ErrorText varchar(30),FileID int)

insert into @a values
(1, 'a', 10),
(1, 'b', 10),
(2, 'a', 11),
(2, 'b', 11),
(2, 'c', 11),
(3, 'c', 12),
(3, 'd', 12)

select * from @a

declare @FileID int, @LineNumber int
declare FileIDCursor cursor for select distinct FileID, LineNumber from @a
declare @AggResult table (LineNumber int, ErrorText varchar(1000), FileID int)

open FileIDCursor
fetch next from FileIDCursor into @FileID, @LineNumber
while @@FETCH_STATUS = 0
begin
declare @Concat varchar(1000) = ''
select @Concat = @Concat + ErrorText + ','
from @a
where FileID = @FileID and linenumber = @LineNumber
insert into @AggResult
select @LineNumber, left(@Concat,len(@Concat)-1), @FileID
fetch next from FileIDCursor into @FileID, @LineNumber
end
close FileIDCursor
deallocate FileIDCursor
select * from @AggResult




Fitz
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14442 Visits: 15980
Have a look at this blog. It's got several different options for concatenating.

John
niladri.primalink
niladri.primalink
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 226
Thanks guys for your input


It is working awesome!!
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26446 Visits: 17557
There is no need to resort to looping for this. You can instead use STUFF.

Thanks Mark for the ddl and sample data.


declare @a table (Linenumber int, ErrorText varchar(30),FileID int)

insert into @a values
(1, 'a', 10),
(1, 'b', 10),
(2, 'a', 11),
(2, 'b', 11),
(2, 'c', 11),
(3, 'c', 12),
(3, 'd', 12)

select Linenumber,
STUFF((select ',' + ErrorText
from @a a2
where a1.Linenumber = a2.Linenumber
order by a2.ErrorText
for xml path('')), 1, 1, '') as ErrorTexts
, FileID
from @a a1
group by Linenumber, FileID



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88096 Visits: 41128
niladri.primalink (9/19/2013)
Thanks guys for your input


It is working awesome!!



"It"? What is "It"? What did you end up using? Please post the code that you ended up using.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
niladri.primalink
niladri.primalink
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 226
Sorry For using the word 'it'.

Actually the code what SScrazy sent to me is working for me.

That's why I wrote it is working for me.

If I hurt you then it is in my unbeknown.
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14442 Visits: 15980
I think what Jeff is getting at, and I agree, is that you shouldn't necessary use the first solution you're given. Mark himself admitted that it's "quick and dirty" and that it won't work very well once your table starts to grow; he even suggested another method. I linked to an article that shows you half a dozen or so ways of doing it. I recommend you evaluate them all to see which works best. Yes, it's more work for you now, but it'll pay in terms of future performance and and of how well prepared you'll be next time you have to implement something like this.

John
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88096 Visits: 41128
niladri.primalink (9/20/2013)
Sorry For using the word 'it'.

Actually the code what SScrazy sent to me is working for me.

That's why I wrote it is working for me.

If I hurt you then it is in my unbeknown.



Thank you for the concern but you didn't hurt me. I'm more concerned about what you ended up using because While Loops and cursors can be quite slow. That's why I wanted to know what you ended up using.

My recommendation would be to have a look at the code that Sean wrote for this. With apologies to Mark, Sean's code will run much more quickly.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search