--> 测试数据:@tb declare @tb table([id] int,[content] varchar(3)) insert @tb select 1,'2月' union all select 2,'3月' union all select 3,'4月' union all select 4,'5月'declare @s varchar(100) select @s=isnull(@s,'')+[content] from @tb where id>=1 and id<4 select @s /* ---------------------------------------------------------------------------------------------------- 2月3月4月(1 行受影响)*/
DECLARE @STR VARCHAR(8000)SELECT @STR=ISNULL(@STR+',','')+name FROM (SELECT DISTINCT content FROM A)AS T where id>1 and id <4SELECT @STR
---考虑可能出现重复 加个distinct DECLARE @STR VARCHAR(8000)SELECT @STR=ISNULL(@STR+',','')+[content] FROM (SELECT DISTINCT content FROM A)AS T where id>1 and id <4SELECT @STR
id content
1 2月
2 3月
3 4月
4 5月
一条sql查询出id=1,2,3,4 时的content ,就是把content都合并起来
declare @tb table([id] int,[content] varchar(3))
insert @tb
select 1,'2月' union all
select 2,'3月' union all
select 3,'4月' union all
select 4,'5月'declare @s varchar(100)
select @s=isnull(@s,'')+[content] from @tb where id>=1 and id<4
select @s
/*
----------------------------------------------------------------------------------------------------
2月3月4月(1 行受影响)*/
where id>1 and id <4SELECT @STR
DECLARE @STR VARCHAR(8000)SELECT @STR=ISNULL(@STR+',','')+[content] FROM (SELECT DISTINCT content FROM A)AS T
where id>1 and id <4SELECT @STR