SQL函数返回值 建一个SQL函数 ALTER FUNCTION fn_3DData() RETURNS varchar(8000) AS Begin declare @s varchar(8000) set @s = ' ' select @s = @s+ ',['+convert(varchar(8),Chid)+']=Sum(case when Chid= '+convert(varchar(8),Chid)+' then Dvalue end) ' from (select Cast(Chid As VarChar) As Chid from DataLine group by DataLine.Chid) a --print 'select Team '+@s+ ' from DataLine group by Team' set @s='SELECT DataLine.Type, DataLine.G, DataLine.Team, DataLine.RS1, DataLine.RS2, DataLine.RS3, DataLine.RDL, DataLine.Rid, DataLine.TimeR '+@s+ ' from DataLine group by DataLine.Type, DataLine.G, DataLine.Team, DataLine.RS1, DataLine.RS2, DataLine.RS3, DataLine.RDL, DataLine.Rid, DataLine.TimeR' --print @s return @s end
在视图中 Select dbo.fn_3DData() 结果是一个 SELECT DataLine.Type, DataLine.G, DataLine.Team, DataLine.RS1, DataLine.RS2, DataLine.RS3, DataLine.RDL, DataLine.Rid, DataLine.TimeR, [1]=Sum(case when Chid= 1 then Dvalue end) from DataLine group by DataLine.Type, DataLine.G, DataLine.Team, DataLine.RS1, DataLine.RS2, DataLine.RS3, DataLine.RDL, DataLine.Rid, DataLine.TimeR 那么怎么执行这条语句呢?