27,579
社区成员
发帖
与我相关
我的任务
分享
declare @T table (项目 varchar(4),金额 varchar(4))
insert into @T
select '学费','20' union all
select '杂费','30' union all
select '书费','2.5'
declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+项目+金额 from @t
select @sql as [合并]
/*
合并
-----------------------
学费20,杂费30,书费2.5
*/
if object_id('test') is not null drop table test
go
create table test(项目 nvarchar(10),金额 float)
go
insert into test
select N'学费', 20 union all
select N'杂费', 30 union all
select N'书费', 2.5
go
declare @s varchar(8000)
set @s = ''
select @s = @s + 项目+convert(varchar(5),金额)+';' from test
select stuff(@s,len(@s),1,'')
/*
(3 行受影响)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
学费20;杂费30;书费2.5
(1 行受影响)
*/
if object_id('test') is not null drop table test
go
create table test(项目 nvarchar(10),金额 float)
go
insert into test
select N'学费', 20 union all
select N'杂费', 30 union all
select N'书费', 2.5
go
select 项目+convert(varchar(5),金额)+';' from test for xml path('')
/*
XML_F52E2B61-18A1-11d1-B105-00805F49916B
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
学费20;杂费30;书费2.5;
*/