34,873
社区成员
发帖
与我相关
我的任务
分享Create table #T([a] int,[b] int)
Insert #T
select 1,2 union all
select 2,3 union all
select 4,5
Go
select name=stuff((select distinct ' '+ltrim(a) from (select a from #t
union all
select B from #T)a for xml path('')),1,1,'')
select a from tb
union
select b from tb
[Quote=引用楼主 tomzjt123 的回复:];with f as
(
select a from t
union
select b from t
)
declare @s varchar(1000)
select @s=isnull(@s+' ' , '')+a from f
select @suse Tempdb
go
SET NOCOUNT ON;
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([a] int,[b] int)
Insert #T
select 1,2 union all
select 2,3 union all
select 4,5
Go
declare @s nvarchar(4000)
set @s=''
select @s=@s+' '+RTRIM(a)
from (select a from #T
union
select b from #T
)t
order by a
print ltrim(@s)
/**/
1 2 3 4 5
*/
Select ' '+lTRIM(a)
FROM
(SELECT a from #T
UNION
Select b from #T)t FOR XML PATH('')
/*
1 2 3 4 5
*/declare @s nvarchar(4000)
set @s=''
select @s=@s+' '+a
from (select a from t
union
select b from t
)t
order by a
print ltrim(@s)