Click here to monitor SSC
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 (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 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.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3692 Visits: 1513
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
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

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

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

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


It is working awesome!!
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16542 Visits: 16997
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 Moden's 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-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44993 Visits: 39879
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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

Group: General Forum Members
Points: 117 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
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7381 Visits: 15095
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-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44993 Visits: 39879
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

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