| 发表于:2007-07-03 17:51:158楼 得分:0 |
加上判斷 --建立測試環境 create table temptable (cola varchar(10), colb int, colc varchar(10)) insert temptable select 'qq1001 ', 1, n 'yuuu ' union all select 'qq1001 ', 2, n 'ttgg ' union all select 'qq1001 ', 3, n 'ffgosdfa ' union all select 'qq1002 ', 1, n 'ewt ' union all select 'qq1002 ', 2, n 'dsfkds ' union all select 'qq1002 ', 3, n ' ' union all select 'qq1002 ', 4, null union all select 'qq1002 ', 5, n 'dasdsa ' union all select 'qq1003 ', 1, n ' ' go --創建函數 create function f_test(@cola varchar(10)) returns varchar(4000) as begin declare @s varchar(4000) select @s = ' ' select @s = @s + ', ' + colc from temptable where cola = @cola and isnull(colc, ' ') != ' ' order by colb select @s = stuff(@s, 1, 1, ' ') return @s end go --測試 select cola, dbo.f_test(cola) as colc from temptable group by cola go --刪除測試環境 drop table temptable drop function f_test --結果 /* cola colc qq1001 yuuu,ttgg,ffgosdfa qq1002 ewt,dsfkds,dasdsa qq1003 null */ | | |
|