关于字符串的链接
create table Test (barcode varchar(20),aa varchar(100))
go
insert into test
values('6900000000007','0.00(2),')
go
insert into test
values('6900000000007','8.00(2),')
go
insert into test
values('6900000000083','95.00(1),')
go
insert into test
values('6900000000083','86.20(1),')
go
select * from test
--------------
barcode aa
'6900000000007' 0.00(2),
'6900000000007' 8.00(2),
'6900000000083' 95.00(1),
'6900000000083' 86.20(1),
现在我想得到这样的结果:
barcode aa
'6900000000007' 0.00(2),8.00(2),
'6900000000083' 95.00(1),86.20(1),
也就是把barcode相同的aa连接起来。。我只想到了用游标的方式,可数据量很大,效率太低了。
有什么好的方法么?感谢~~
[解决办法]
create table Test (barcode varchar(20),aa varchar(100))
go
insert into test
values('6900000000007','0.00(2),')
go
insert into test
values('6900000000007','8.00(2),')
go
insert into test
values('6900000000083','95.00(1),')
go
insert into test
values('6900000000083','86.20(1),')
go
select barcode,
aa=stuff((select ''+aa from test where barcode=a.barcode for xml path('')),1,1,'')
from test a group by barcode
/*
barcode aa
-------------------- -----------------
6900000000007 0.00(2),8.00(2),
6900000000083 5.00(1),86.20(1),
create table Test (barcode varchar(20),aa varchar(100))
go
insert into test
values('6900000000007','0.00(2),')
go
insert into test
values('6900000000007','8.00(2),')
go
insert into test
values('6900000000083','95.00(1),')
go
insert into test
values('6900000000083','86.20(1),')
go
create function getstr(@id varchar(20))
returns varchar(100)
as
begin
declare @str varchar(100)
set @str=''
select @str=@str+aa from Test where barcode=@id
return @str
end
select barcode,dbo.getstr(barcode) from test group by barcode
/*
barcode
-------------------- ------------------
6900000000007 0.00(2),8.00(2),
6900000000083 95.00(1),86.20(1),