如何将查询的记录都合并成为一个记录呢?其实实现的SQL语句写法并不复杂,下面就为您列出该SQL语句,希望对您学习SQL语句有所启迪。
create table t
(tableid nchar(30))
insert t
select ’T1’ union all
select ’T2’ union all
select ’T3’ union all
select ’T4’ union all
select ’T5’ union all
select ’T6’
go
create function f_he()
returns @t table(col varchar(50))
as
begin
declare @sql varchar(50)
set @sql=’’
select @sql=@sql+ltrim(rtrim(tableid)) from t
insert @t values (@sql)
return
end
go
select * from t
select * from dbo.f_he()
drop function f_he
drop table t
col
--------------------------------------------------
T1T2T3T4T5T6
(所影响的行数为 1 行)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
【编辑推荐】