将行连接到一列sybase

时间:2018-06-22 09:06:25

标签: sql sybase

我当前数据库的功能有限。我有一种情况需要将行连接到SYBASE中的一列

LETTER
A
B
C
D

EXPECTED RESULT
A,B,C,D

能帮我吗?

1 个答案:

答案 0 :(得分:0)

我使用了此表格...根据需要进行更改!

declare @w_caracter_val varchar(2),
        @w_contador smallint,
        @w_registros smallint,
        @w_caracterFinal varchar(30)
--Crear tabla temporal        
create table #sb_caracteres   
             (se_id            int identity,
              codigo           varchar(10))
--insert a la tabla #                                
insert into #sb_caracteres
           (codigo)
    select  codigo        
     from cr_corresp_sib  
    where tabla = 'T260'   

--select * from #sb_caracteres
select @w_registros = (select count(1)from  #sb_caracteres)
select @w_contador = 1
select @w_caracterFinal= ''

while @w_contador <= @w_registros 
begin
    select   @w_caracter_val = codigo             
    from #sb_caracteres  
    where se_id = @w_contador

print 'codigo %1!, %2!',  @w_contador, @w_caracter_val

--select @w_caracterFinal= concat(@w_caracterFinal,@w_caracter_val)

select @w_caracterFinal = ltrim(rtrim(@w_caracterFinal)) + ltrim(rtrim(@w_caracter_val))   

select @w_caracterFinal
--print 'registros %1!',  @w_caracterFinal
Select @w_contador = @w_contador +1
end