SQL简单问题……
时间:2011-12-16
来源:互联网
小弟刚学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语句有问题么。
求前辈帮忙,在此谢。
写了个存储过程,如下:
-------------------------------------------------------------------
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语句有问题么。
求前辈帮忙,在此谢。
作者: kyle_huang 发布时间: 2011-12-16
传入参数ID 00001,得到的应该是00001的话,改这儿:
d_Goods.Id like @ID+'%'
为
d_Goods.Id = @ID
d_Goods.Id like @ID+'%'
为
d_Goods.Id = @ID
作者: qianjin036a 发布时间: 2011-12-16
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28