行转列生成字段串然后执行
declare @sql nvarchar(max)
set @sql=' select [分组的ID] '
--临时变量,用来保存游标值
declare @title nvarchar(200)
declare y_curr cursor for
select title from [列的字段表]
open y_curr
fetch next from y_curr into @title
while(@@fetch_status=0)
begin
--print (@title)
set @sql+=' ,MAX(CASE [字段名称] WHEN '''+@title+''' THEN [字段对应的值] ELSE '''' END) as '''+@title+''''
fetch next from y_curr into @title
end
close y_curr
deallocate y_curr
set @sql+=' from [行转列的表] group by [分组的ID]'
--print @sql
EXEC sp_executesql @sql --执行