简体   繁体   English

我需要将数据从2列合并到另一列。 然后将添加的列的所有行合并到另一张表的一个单元格中

[英]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

I need to merge data from 2 columns to another column. 我需要将数据从2列合并到另一列。 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 [ http://i.stack.imgur.com/srcrp.png] 要查看图像,请点击[ http://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 就像这张图片所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM