34,588
社区成员
发帖
与我相关
我的任务
分享
declare @name nvarchar(2000)
select @name = isnull(@name + ';','') + 列B
from 表
where 列A = 1
select @name as name
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (A int,B nvarchar(4))
insert into [TB]
select 1,'张三' union all
select 1,'李四' union all
select 1,'王五' union all
select 1,'赵六'
select * from [TB]
drop function dbo.f_str
create function dbo.f_str(@id int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + ';' + cast(b as varchar) from tb where a = @id
set @str = right(@str , len(@str) - 1)
return @str
end
go
select a,dbo.f_str(a) from TB group by a
/*
a
----------- ----------------------------------------------------------------------------------------------------
1 张三;李四;王五;赵六
(1 行受影响)
declare @name nvarchar(2000)
select @name = isnull(@name ,'') + 列B + ';'
from 表
where 列A = 1
select @name as name
declare @name nvarchar(2000)
select @name = isnull(@name + ';','') + 列B
from 表
where 列A = 1
select @name as name