如何从SQL CE中的表构建逗号分隔列表?

时间:2013-12-06 06:10:51

标签: sql select sql-server-ce sql-server-ce-4

如何从SQL CE中的表构建逗号分隔列表?

我有一个名为Group的表,它有两列ID和Name

我想从Group表中选择一个逗号分隔的字符串。

因此,如果我在组表中有3条记录

ID  | Name 
 1  | Msh
 2  | Nsh
 3  | Lsh 

我想得到一个逗号分隔的所有三个名字的列表,例如Msh,Nsh,Lsh

我怎样才能完成这项工作SQL CE

2 个答案:

答案 0 :(得分:0)

试试这个..

DECLARE @COMMA VARCHAR(MAX)
 SET @COMMA =''
 SELECT @COMMA =@COMMA +name+',' FROM yourtablename
 SELECT SUBSTRING(@COMMA,0,LEN(@COMMA))

答案 1 :(得分:0)

    You can develop a simple logic in SQL. This is a dummy code you can try and modify the code as per your requirements.

    declare
        i varchar2(100);
        j varchar2(100);
        begin
        for i in (select name from avrajit)
        loop
        j:=i.name||','||j;
        end loop;
        dbms_output.put_line(j);
        end;

---------------------------------------
OUTPUT
---------------------------------------

Hitesh,Sushil2,Mukul,Shyam,Nikheel,Avrajit,Sushil,

Statement processed.