1

I need to merge data from 2 columns to another column. And then merge all rows of that added column into one cell in another table..

Added two columns to one a column.!! to see image click [https://i.stack.imgur.com/srcrp.png]

code used :

SELECT (CustomerName + ' ' + ContactName) as onecolumn from company_PR

Now I need to merge all rows of that added column into one cell in another table. like shown in this image

cbass
  • 2,548
  • 2
  • 27
  • 39
Siju Ninan
  • 47
  • 1
  • 5
  • `insert into AllTogether (onecolumn) SELECT (CustomerName + ' ' + ContactName) as onecolumn from company_PR` ? – SmartDev Oct 22 '14 at 20:15
  • This is a common SQL related question, one example with answer [is here](http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string). – Iztoksson Oct 22 '14 at 20:32

1 Answers1

1

If you're doing this at the server side then one way of doing this is to use a cursor.

DECLARE @ListEntry varchar(100)
DECLARE @List varchar(1000)
DECLARE ENTRY_CURSOR CURSOR  FOR 
SELECT (CustomerName + ' ' + ContactName) as ListEntry from company_PR

SET @List = ''
OPEN ENTRY_CURSOR 
FETCH NEXT FROM ENTRY_CURSOR INTO @ListEntry 
WHILE @@FETCH_STATUS =0
BEGIN
    IF @List ='' 
    SET @List = @ListEntry  --Don't put a comma before the first entry on the list
    ELSE
    SET @List = @List +', '+ @ListEntry

    FETCH NEXT FROM ENTRY_CURSOR INTO @ListEntry 
END
CLOSE ENTRY_CURSOR 
DEALLOCATE ENTRY_CURSOR 
SELECT @List AS list
Tom Page
  • 1,211
  • 1
  • 7
  • 8