高分请教高手,应该如何写SQL语句或者存储过程,把表A的格式存成表B的格式?
表A   
 SID         ACCOUNT   
 1                     王大   
 1                     张三   
 2                     李四   
 2                     钱武          
 表B   
 SID      ACCOUNT   
 1            王大,张三,   
 2            李四,钱五,   
------解决方案--------------------需要借助函数处理。
------解决方案--------------------函数,参考:   
 --测试数据 
 create table csdn(id int,txt varchar(10)) 
 insert csdn 
 select 1, 'a ' union all 
 select 1, 'b ' union all 
 select 1, 'c ' union all 
 select 2, 'aa ' union all 
 select 2, 'bb ' union all 
 select 2, 'cc ' union all 
 select 3, 'aaa ' union all 
 select 3, 'bbb ' 
 --select * from csdn 
 go   
 create function Gettxt(@id int) 
 returns varchar(8000) 
 as 
 begin 
     declare @s varchar(8000) 
 set @s= ' ' 
 select @s=@s + ', ' +txt from csdn where id=@id 
 --return @s 
 return  stuff(@s,1,1, ' ') 
 end 
 go   
 select id,dbo.Gettxt(id) txt from csdn group by id 
 go   
 drop function Gettxt 
 drop table csdn
------解决方案--------------------create function fn_test(@sid int) 
 returns varchar(100) 
 AS 
 begin 
 declare @str varchar(100) 
 set @str= ' ' 
 select @str=@str+ACCOUNT+ ', ' from 表A where sid=@sid 
 return @str 
 end   
 GO 
 insert into 表B 
 select SID ,fn_test(SID) as ACCOUNT  
 from 表A 
 group by SID
------解决方案--------------------declare @t table( sid int ,account varchar(10)) 
 insert into @t select 1, '王大 ' 
 union all select 1, '张三 ' 
 union all select 2, '李四 ' 
 union all select 2, '钱武 '   
 SELECT * 
 FROM( 
     SELECT DISTINCT  
         sid 
     FROM @t 
 )A 
 OUTER APPLY( 
     SELECT  
         [values]= STUFF(REPLACE(REPLACE( 
             ( 
                 SELECT account FROM @t N 
                 WHERE sid= A.sid 
                 FOR XML AUTO 
             ),  ' <N account= " ',  ', '),  ' "/>  ',  ' '), 1, 1,  ' ')+ ', ' 
 )N   
 /* 
 sid         values 
 ----------- ------------- 
 1           王大,张三, 
 2           李四,钱武,   
 (2 行受影响)   
 */