如下修改即可 declare @str varchar(1000)
 set @str='select id,visit '
 select @str=@str+',min(case when goods='+ Cast(goods As Varchar) +' then goods_id else Null end) as goods_id'+ Cast(goods As Varchar)
+',min(case when goods='+ Cast(goods As Varchar) +' then name else Null end) as goods'+ Cast(goods As Varchar)
 from table_goods group by goods
 select @str=@str+' from table_goods group by id,visit order by id,visit'
 exec(@str)