请教大家一个数据库查询的问题,在线等...(50分)

  • 主题发起人 主题发起人 mwp316
  • 开始时间 开始时间
M

mwp316

Unregistered / Unconfirmed
GUEST, unregistred user!
表test<br>id_no &nbsp; &nbsp;id_name<br>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a<br>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b<br>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c<br>2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d<br>2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f<br>2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e<br>3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c<br>3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b<br>通过SQL查询,实现下列数据<br>id_no &nbsp; &nbsp;id_name<br>1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a,b,c<br>2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d,f,e<br>3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c,d
 
写存储过程可以实现
 
确实比较麻烦,一时想不到好的方法。<br><br>存储过程肯定可以的。
 
老问题了,可以查以往的记录,基本上都是存储过程的。
 
刚刚做了一个同样的问题呵呵<br>---- &nbsp; &nbsp; GetMaterialInfo 'tao080711001'<br><br>CREATE &nbsp;procedure &nbsp;GetMaterialInfo2<br>&nbsp; @pnno varchar(20)<br>&nbsp;as <br><br>&nbsp;create table #tmp(<br>id &nbsp; decimal(18,0) ,<br>wlname &nbsp;varchar(20) ,<br>spec &nbsp;varchar(50),<br>part varchar(20),<br>unituse &nbsp;decimal(18,4),<br>unit &nbsp;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 &nbsp;id,wlname,spec,part,unituse,unit,pnnoqty,bz ,unituse*pnnoqty &nbsp;as &nbsp;alluse <br>into #tmp2<br>from &nbsp; <br>production_wl where pnno= @pnno &nbsp;<br><br>insert into #tmp(wlname,spec,part,unituse,unit,bz)<br>select &nbsp;distinct wlname,spec,part,unituse,unit ,bz<br>from &nbsp; #tmp2 &nbsp; <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 &nbsp;for <br>&nbsp;select distinct wlname from #tmp2 &nbsp;<br>&nbsp; open &nbsp;material_cursor<br>fetch next from &nbsp;material_cursor &nbsp;into &nbsp;@wlname<br>while @@fetch_status = 0 <br>&nbsp;begin <br>&nbsp; &nbsp; &nbsp; set @i = 1<br>&nbsp; &nbsp; declare qty_cursor cursor for<br>&nbsp; &nbsp;select &nbsp; alluse from &nbsp;#tmp2 &nbsp;where wlname = @wlname <br>&nbsp; &nbsp;open &nbsp;qty_cursor &nbsp;<br>&nbsp; fetch next from &nbsp;qty_cursor into @alluse<br>&nbsp; &nbsp; while &nbsp;@@fetch_status = &nbsp;0 <br>&nbsp; &nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;set @s=convert(varchar(1),@i)<br>&nbsp; &nbsp; &nbsp; &nbsp;if @i&lt;5<br>&nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; set @Str = 'update #tmp &nbsp;set colorqty' + @s + '= '''+cast(@alluse as varchar)+''' ' <br>&nbsp; &nbsp; &nbsp; ---set @Str = 'update #tmp &nbsp;set colorqty' + @s + '= '+@alluse +' ' &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; print(@str)<br>&nbsp; &nbsp; &nbsp; &nbsp; exec(@str)<br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; set @i = @i+1 &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; fetch next from &nbsp;qty_cursor into @alluse<br>&nbsp; &nbsp; &nbsp; end<br><br>&nbsp; &nbsp;close &nbsp;qty_cursor<br>&nbsp; &nbsp;deallocate qty_cursor<br>&nbsp; fetch next from &nbsp;material_cursor &nbsp;into &nbsp;@wlname<br>&nbsp;<br>end;<br>close &nbsp; material_cursor<br>deallocate &nbsp;material_cursor<br><br>select wlname,spec,part,unituse,unit,bz,colorqty1, colorqty2,colorqty3,colorqty4 from &nbsp;#tmp<br><br>GO
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部