刚刚做了一个同样的问题呵呵<br>---- GetMaterialInfo 'tao080711001'<br><br>CREATE procedure GetMaterialInfo2<br> @pnno varchar(20)<br> as <br><br> create table #tmp(<br>id decimal(18,0) ,<br>wlname varchar(20) ,<br>spec varchar(50),<br>part varchar(20),<br>unituse decimal(18,4),<br>unit varchar(10), <br>pnnoqty decimal(18,2),<br>bz varchar(50),<br>colorqty1 decimal(18,2),<br>colorqty2 decimal(18,2),<br>colorqty3 decimal(18,2),<br>colorqty4 decimal(18,2)<br>)<br><br><br><br><br>select id,wlname,spec,part,unituse,unit,pnnoqty,bz ,unituse*pnnoqty as alluse <br>into #tmp2<br>from <br>production_wl where pnno= @pnno <br><br>insert into #tmp(wlname,spec,part,unituse,unit,bz)<br>select distinct wlname,spec,part,unituse,unit ,bz<br>from #tmp2 <br><br><br>declare <br>@wlname varchar(30),<br>@alluse decimal(18,2),<br>@i int,<br>@str varchar(100)<br><br>declare @s varchar(20)<br><br>declare material_cursor cursor for <br> select distinct wlname from #tmp2 <br> open material_cursor<br>fetch next from material_cursor into @wlname<br>while @@fetch_status = 0 <br> begin <br> set @i = 1<br> declare qty_cursor cursor for<br> select alluse from #tmp2 where wlname = @wlname <br> open qty_cursor <br> fetch next from qty_cursor into @alluse<br> while @@fetch_status = 0 <br> begin <br> <br> set @s=convert(varchar(1),@i)<br> if @i<5<br> <br> set @Str = 'update #tmp set colorqty' + @s + '= '''+cast(@alluse as varchar)+''' ' <br> ---set @Str = 'update #tmp set colorqty' + @s + '= '+@alluse +' ' <br> print(@str)<br> exec(@str)<br> <br> set @i = @i+1 <br> fetch next from qty_cursor into @alluse<br> end<br><br> close qty_cursor<br> deallocate qty_cursor<br> fetch next from material_cursor into @wlname<br> <br>end;<br>close material_cursor<br>deallocate material_cursor<br><br>select wlname,spec,part,unituse,unit,bz,colorqty1, colorqty2,colorqty3,colorqty4 from #tmp<br><br>GO