库存的SQL语句(50分)

  • 主题发起人 主题发起人 cooldren
  • 开始时间 开始时间
C

cooldren

Unregistered / Unconfirmed
GUEST, unregistred user!
基础表<br>&nbsp; &nbsp; 商品编号 &nbsp; 商品名称<br>仓库表<br>&nbsp; &nbsp; 仓库编号 &nbsp; 仓库名称<br>库存表<br>&nbsp; &nbsp; 商品编号 &nbsp; 仓库编号 &nbsp; 库存数量<br>&nbsp; &nbsp; &nbsp; &nbsp;01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;01 &nbsp; &nbsp; &nbsp; &nbsp; 20<br>&nbsp; &nbsp; &nbsp; &nbsp;01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;02 &nbsp; &nbsp; &nbsp; &nbsp; 30<br>&nbsp; &nbsp; &nbsp; &nbsp;01 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;03 &nbsp; &nbsp; &nbsp; &nbsp; 11<br><br>想这样显示<br>&nbsp; &nbsp; 商品编号 &nbsp; 仓库01 &nbsp; 仓库02 &nbsp; &nbsp;仓库03<br><br>高手指导一下,谢谢<br>&nbsp; &nbsp; &nbsp; &nbsp;01 &nbsp; &nbsp; &nbsp; &nbsp;20 &nbsp; &nbsp; &nbsp; 30 &nbsp; &nbsp; &nbsp; &nbsp;11
 
select case
 
用动态交叉表:<br><br>declare @WarehouseID varchar(5), @WarehouseName varchar(20), @sql nvarchar(2000)<br>set @sql='select 商品编号'<br>declare cur cursor for select 仓库编号, 仓库名称 from 仓库表<br>open cur<br>fetch next from cur into @WarehouseID, @WarehouseName<br>while @@fetch_status=0<br>begin<br>&nbsp; set @sql=@sql+', case 仓库编号 when '''+@WarehouseID+''' then 库存数量 else 0 end as '+@WarehouseName<br>&nbsp; fetch next from cur into @WarehouseID, @WarehouseName<br>end<br>close cur<br>deallocate cur<br>set @sql=@sql+' from 库存表'<br>exec sp_executesql @sql<br><br>大致就是这样。你自己修补一下就可以了。
 
高人,佩服。
 
后退
顶部