教您将记录合并的SQL语句写法


  本文标签:SQL语句

  如何将查询的记录都合并成为一个记录呢?其实实现的SQL语句写法并不复杂,下面就为您列出该SQL语句,希望对您学习SQL语句有所启迪  。

  1. create table t   
  2. (tableid nchar(30))   
  3. insert t   
  4. select ’T1’ union all   
  5. select ’T2’ union all   
  6. select ’T3’ union all   
  7. select ’T4’ union all   
  8. select ’T5’ union all   
  9. select ’T6’   
  10. go   
  11. create function f_he()   
  12. returns @t table(col varchar(50))   
  13. as   
  14. begin   
  15. declare @sql varchar(50)   
  16. set @sql=’’   
  17. select @sql=@sql+ltrim(rtrim(tableid)) from t   
  18. insert @t values (@sql)   
  19. return   
  20. end   
  21. go   
  22. select * from t   
  23. select * from dbo.f_he()   
  24. drop function f_he   
  25. drop table t   
  26. col   
  27. --------------------------------------------------   
  28. T1T2T3T4T5T6   
  29. (所影响的行数为 1 行)