declare @s varchar(1000) select @s=isnull(@s,'')+ from tb select @s
declare @s varchar(100) select @s=isnull(@s+',','')+ from tb select @s
declare @s varchar(1000) select @s=isnull(@s,'')+ from tb select @s
如果是sql205及以上版本,可以这样 ---测试数据--- if object_id('[tb]') is not null drop table [tb] go create table [tb]([id] int,[Mark] varchar(3)) insert [tb] select 1,'A01' union all select 2,'A02' union all select 3,'A03' union all select 4,'A04'
---查询--- select stuff((select ','+ from tb for xml path('')),1,1,'') ---结果------------------------------- A01,A02,A03,A04(1 行受影响)
declare @s varchar(100) select @s=isnull(','+@s,'')+ from tb select @s
可惜是sql2000的,用逗号隔开呢?
declare @s varchar(1000) select @s=isnull(@s,'')++',' from tb select substring(@s,0,len(@s))
select @s=isnull(@s,'')+ from tb
select @s
select @s=isnull(@s+',','')+ from tb select @s
select @s=isnull(@s,'')+ from tb
select @s
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[Mark] varchar(3))
insert [tb]
select 1,'A01' union all
select 2,'A02' union all
select 3,'A03' union all
select 4,'A04'
---查询---
select
stuff((select ','+ from tb for xml path('')),1,1,'')
---结果-------------------------------
A01,A02,A03,A04(1 行受影响)
select @s=isnull(','+@s,'')+ from tb select @s
select @s=isnull(@s,'')++',' from tb
select substring(@s,0,len(@s))