SQL简单问题……
小弟刚学SQL,很多地方不明白,求前辈支支招,谢谢了。
写了个存储过程,如下:
-------------------------------
if object_id('test') is not null --一二句判断存储过程是否存在
drop procedure test
go --批处理,上下批有关系的时候go不能省略
create procedure test --创建存储过程
@ID varchar(50) --输入
as
select '商品ID' = d_Goods.Id, '商品PersonCode' = d_Goods.PersonCode, '商品FullName' = d_Goods.FullName,
'仓库ID' = d_Stock.ID, '仓库PersonCode' = d_Stock.PerSonCode, '仓库FullName' = d_Stock.FullName,
'库存数量' = sum(d_StockGoods.Num), '库存金额' = sum(d_StockGoods.Amount)
from D_Stock inner join d_StockGoods on d_StockGoods.sId = D_Stock.Id
inner join d_Goods on d_StockGoods.gID = d_Goods.ID and d_Goods.Id like @ID+'%'
group by d_Goods.Id, d_Goods.PersonCode, d_Goods.FullName, d_Stock.ID, d_Stock.PerSonCode, d_Stock.FullName
go
execute test '00001'
-------------------------------
得到的效果是:(传入参数:商品ID,返回数据:该商品在各个仓库中的分布情况)
商品ID 商品PersonCode 商品FullName 仓库ID 仓库PersonCode 仓库FullName 库存数量 库存金额
-------------------------------------------------------------
0000100001 KB00001 康柏 00001 00001 caigou 8 8
0000100001 KB00001 康柏 00002 00002 qita 100 1000
0000100002 HP00002 惠普001 00002 00002 qita 10 80
-------------------------------------------------------------
但是,这不是我想要的效果,我想要的是:传入参数ID 00001,得到的应该是:
商品ID 商品PersonCode 商品FullName 仓库ID 仓库PersonCode 仓库FullName 库存数量 库存金额
-------------------------------------------------------------
00001 HP00001 惠普 00001 00001 caigou 8 8
(-----接着上面的----) 仓库ID 仓库PersonCode 仓库FullName 库存数量 库存金额
-----------------------------------------------------
00002 00002 qita 110 1080
-----------------------------------------------------
-------------------------------分割线---------------------------------------
00001与0000100001、0000100002的关系是(有个pID,0000100001的pID是00001),也就是我输入00001的时候,出现是上面的。不知道我说清楚没有,或者我上面的SQL语句有问题么。
求前辈帮忙,在此谢。
------解决方案--------------------传入参数ID 00001,得到的应该是00001的话,改这儿:
d_Goods.Id like @ID+'%'
为
d_Goods.Id = @ID
------解决方案--------------------SQL code
if object_id('test') is not null --一二句判断存储过程是否存在
drop procedure test
go --批处理,上下批有关系的时候go不能省略
create procedure test --创建存储过程
@ID varchar(50) --输入
as
select '商品ID' = d_Goods.Id, '商品PersonCode' = d_Goods.PersonCode, '商品FullName' = d_Goods.FullName,
'仓库ID' = d_Stock.ID, '仓库PersonCode' = d_Stock.PerSonCode, '仓库FullName' = d_Stock.FullName,
'库存数量' = sum(d_StockGoods.Num), '库存金额' = sum(d_StockGoods.Amount)
from D_Stock inner join d_StockGoods on d_StockGoods.sId = D_Stock.Id
inner join d_Goods on d_StockGoods.gID = d_Goods.ID and pId=@ID --应该是这改成pId=@ID吧,你比较的是pId,为什么要用d_Goods.Id like @ID+'%'
group by d_Goods.Id, d_Goods.PersonCode, d_Goods.FullName, d_Stock.ID, d_Stock.PerSonCode, d_Stock.FullName
go
execute test '00001'
------解决方案--------------------
SQL code
create procedure test
@ID varchar(50)
as
select '商品ID' = d_Goods.Id, '商品PersonCode' = d_Goods.PersonCode, '商品FullName' = d_Goods.FullName,
'仓库ID' = d_Stock.ID, '仓库PersonCode' = d_Stock.PerSonCode, '仓库FullName' = d_Stock.FullName,
'库存数量' = sum(d_StockGoods.Num), '库存金额' = sum(d_StockGoods.Amount)
from D_Stock inner join d_StockGoods on d_StockGoods.sId = D_Stock.Id
inner join d_Goods on d_StockGoods.gID = d_Goods.ID and pId=@ID group by d_Goods.Id, d_Goods.P