同意Corn3,如果是不确定行的数据转为列则和上面的方法类似,但是要拼成类似上面的语句<br>下面是个类似的行转列的语句<br>declare @str varchar(8000)<br>declare @str1 varchar(8000)<br>declare @kmbh varchar(8000)<br>declare @kmmc varchar(8000)<br> declare @bh int <br>set @str1= ''<br>set @str= ''<br>set @bh= 1 <br>declare fff cursor for select distinct kmbh,kmmc from l_fzzxmtotal where dwmc='部门辅助库-1005AA10000000001HD6' order by kmbh<br>open fff <br>fetch next from fff into @kmbh,@kmmc<br><br>set @str='select dwbh,dwmc, '<br>while @@fetch_status = 0 <br>begin<br>if len(@str) > 7000 <br> begin <br> set @str1 = @str1+@str<br> set @str = ''<br> end <br> else <br> begin <br> set @str=@str+'sum(case when kmbh like '''+@kmbh+'%'' then jfje else 0 end) as '''+cast(@bh as varchar(1000))+''','<br> end <br> set @bh= @bh + 1<br> fetch next from fff into @kmbh,@kmmc<br>end<br>close fff<br>deallocate fff <br> set @str= left(@str,len(@str)-1)<br>set @str=@str+',sum(jfje) je into l_fzzxmtotal_result from l_fzzxmtotal where 1=1' <br> set @str=@str+ ' and dwmc = ''部门辅助库-1005AA10000000001HD6'''<br> set @str=@str+ ' and pzrq >=''20060101'' and pzrq <= ''20061231'''<br> set @str=@str+ ' group by dwbh, dwmc'<br> print @str<br> exec (@str1+@str)