---------------------------------------------------- /*如何将一列中所有的值一行显示 数据源 a b c d e 结果 a,b,c,d,e */create table tb(col varchar(20)) insert tb values ('a') insert tb values ('b') insert tb values ('c') insert tb values ('d') insert tb values ('e') go--方法一 declare @sql varchar(1000) set @sql = '' select @sql = @sql + t.col + ',' from (select col from tb) as t set @sql='select result = ''' + left(@sql , len(@sql) - 1) + '''' exec(@sql) /* result ---------- a,b,c,d,e, */--方法二 declare @output varchar(8000) select @output = coalesce(@output + ',' , '') + col from tb print @output /* a,b,c,d,e */drop table tb-------------
--> Title : Generating test data --> Author : wufeng4552 --> Date : 2010-02-04 13:57:59
if not object_id('tb') is null drop table tb Go Create table tb([id] int,[name] nvarchar(3)) Insert tb select 1,N'aaa' union all select 2,N'bbb' union all select 3,N'cc' union all select 4,N'd' Go declare @s varchar(1000) select @s=isnull(@s+',','')+ltrim(ID)from tb select @s=isnull(@s+',','')+ltrim([Name])from tb select @s /* 1,2,3,4,aaa,bbb,cc,d(1 個資料列受到影響) */
Create table tb([id] int,[name] nvarchar(3)) Insert tb select 1,N'aaa' union all select 2,N'bbb' union all select 3,N'cc' union all select 4,N'd' Goselect top 1id=SUBSTRING((select ','+cast(id as varchar) from tb for xml path('')),2,100000), name=SUBSTRING((select ','+name from tb for xml path('')),2,100000)from tb
/*如何将一列中所有的值一行显示
数据源
a
b
c
d
e
结果
a,b,c,d,e
*/create table tb(col varchar(20))
insert tb values ('a')
insert tb values ('b')
insert tb values ('c')
insert tb values ('d')
insert tb values ('e')
go--方法一
declare @sql varchar(1000)
set @sql = ''
select @sql = @sql + t.col + ',' from (select col from tb) as t
set @sql='select result = ''' + left(@sql , len(@sql) - 1) + ''''
exec(@sql)
/*
result
----------
a,b,c,d,e,
*/--方法二
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + col from tb
print @output
/*
a,b,c,d,e
*/drop table tb-------------
--> Author : wufeng4552
--> Date : 2010-02-04 13:57:59
if not object_id('tb') is null
drop table tb
Go
Create table tb([id] int,[name] nvarchar(3))
Insert tb
select 1,N'aaa' union all
select 2,N'bbb' union all
select 3,N'cc' union all
select 4,N'd'
Go
declare @s varchar(1000)
select @s=isnull(@s+',','')+ltrim(ID)from tb
select @s=isnull(@s+',','')+ltrim([Name])from tb
select @s
/*
1,2,3,4,aaa,bbb,cc,d(1 個資料列受到影響)
*/
declare @s varchar(1000) declare @s1 varchar(1000)
select @s=isnull(@s+',','')+ltrim(ID)from tb
select @s1=isnull(@s1+',','')+ltrim([name])from tb
select @s,@s1id name
------------------------ ---------------------------
1,2,3,4,5 a,b,c,d,e(1 行受影响)
insert @t select 1,'aaa'
insert @t select 2,'bbb'
insert @t select 3,'cc'
insert @t select 4,'d' declare @id varchar(20),@name varchar(20)
select @id='',@name=''select @id=@id+','+cast(id as varchar(10)),@name=@name+','+name
from @tselect right(@id,len(@id)-1) as id,right(@name,len(@name)-1) as name
/*
id name
-------------------- --------------------
1,2,3,4 aaa,bbb,cc,d
*/
可以用生成一个字符串的方法为:
declare @s varchar(1000)
select @s=isnull(@s+',','')+ltrim(ID)from tb
select @s=isnull(@s+',','')+ltrim([Name])from tb
select @s如果字符数据太长,那就不行了.
Create table tb([id] int,[name] nvarchar(3))
Insert tb
select 1,N'aaa' union all
select 2,N'bbb' union all
select 3,N'cc' union all
select 4,N'd'
Goselect top 1id=SUBSTRING((select ','+cast(id as varchar) from tb for xml path('')),2,100000),
name=SUBSTRING((select ','+name from tb for xml path('')),2,100000)from tb