sqlServer 存储过程问题
sqlServer 存储过程问题
现有一张表数据大约是这样的(3条记录,3条记录的前三列记录都相等,后三列不等)
a b c m1 m2 m3
a b c m4 m5 m6
a b c m7 m8 m9
怎样把结果弄成
a b c m1 m2...m9 SQL?Server 存储 行业数据 T_SQL ORACLE
[解决办法]
每次取一条记录JOIN一下获得所有字段就可以了
[解决办法]
create table tb(col1 varchar(5),
col2 varchar(5),
col3 varchar(5),
col4 varchar(5),
col5 varchar(5),
col6 varchar(5)
)
insert into tb
select 'a','b','c','m1','m2','m3'
union all select 'a','b','c','m4','m5','m6'
union all select 'a','b','c','m7','m8','m9'
select * from tb
declare @s varchar(1000)
set @s=''
select @s=@s+col1+char(9)+col2+char(9)+col3+char(9)
from (select distinct col1,col2,col3 from tb)t
select @s=@s+col4+char(9)+col5+char(9)+col6+char(9)
from tb
print @s
/*
abcm1m2m3m4m5m6m7m8m9
*/