当前位置: 代码迷 >> Sql Server >> 判断库存是数量是否OK,这样写如何不行那.
  详细解决方案

判断库存是数量是否OK,这样写如何不行那.

热度:69   发布时间:2016-04-24 20:04:43.0
判断库存是数量是否OK,这样写怎么不行那.???

       if exists(select 1 from 
(select skucode,ordcom,skupch,sum(qty) qty from ord where ordno='ZQAL201310280001'  group by skucode,ordcom,skupch ) t1,
(select skucode,recname,skupch,sum(skuqty) qty from sellord group by skucode,recname,skupch ) t2
where t1.skucode = t2.skucode and  t1.ordcom=t2.recname and t1.skupch=t2.skupch
and t1.qty > t2.qty)
begin
  print '库存不足' 
end
else
begin
  print '库存OK'
end
/*库存OK*/
以下是数据!
select skucode,ordcom,skupch,sum(qty) qty from ord where ordno='ZQAL201310280001'  group by skucode,ordcom,skupch

/*skucode ordcom skupch qty
185469         科技有限公司 31
570798  科技有限公司 SA9316DW 5*/

select skucode,recname,skupch,sum(skuqty) qty from sellord group by skucode,recname,skupch
/*skucode recname skupch qty
185469 科技有限公司 277
185470 科技有限公司 60
570311 科技有限公司 150
571818 科技有限公司 1500
982762 科技有限公司 SA63102447 1
982762 科技有限公司 SA631024GB 2
982762 科技有限公司 SA631024GH 1*/



ord 是出库据
sellord  是库存数量

库存里都没有为什么还是OK?土豪们,来看看吧!

------解决方案--------------------
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-10-28 10:21:44
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
-- Dec 28 2012 20:23:12 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[sellord]
if object_id('[sellord]') is not null drop table [sellord]
go 
create table [sellord]([skucode] int,[recname] varchar(12),[skupch] varchar(10),[qty] int)
insert [sellord]
select 185469,'科技有限公司','277',null union all
select 185470,'科技有限公司','60',null union all
select 570311,'科技有限公司','150',null union all
select 571818,'科技有限公司','1500',null union all
select 982762,'科技有限公司','SA63102447',1 union all
select 982762,'科技有限公司','SA631024GB',2 union all
select 982762,'科技有限公司','SA631024GH',1
--> 测试数据:[ord]
if object_id('[ord]') is not null drop table [ord]
go 
create table [ord]([skucode] int,[ordcom] varchar(12),[skupch] varchar(8),[qty] int)
insert [ord]
select 185469,'科技有限公司','31',null union all
select 570798,'科技有限公司','SA9316DW',5
--------------开始查询--------------------------

IF EXISTS (
select 1 from (SELECT skucode,recname ,SUM([qty])[qty] FROM  [sellord] GROUP BY skucode,recname)a
WHERE not  EXISTS (SELECT 1 FROM (SELECT skucode,ordcom,SUM([qty])[qty] FROM [ord] GROUP BY  skucode,ordcom) b WHERE a.skucode=b.skucode AND a.recname=b.ordcom AND a.qty>b.qty))
BEGIN
PRINT '库存不足'
END 
ELSE
BEGIN
PRINT '库存足'
END
----------------结果----------------------------
/* 
库存不足

*/

------解决方案--------------------
----------------------------------------------------------------
-- Author  :fredrickhu(小F,向高手学习)
-- Date    :2013-10-28 10:48:55
-- Verstion:
--      Microsoft SQL Server 2012 - 11.0.2100.60 (X64) 
-- Feb 10 2012 19:39:15 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[发货表]
if object_id('[发货表]') is not null drop table [发货表]
go 
create table [发货表]([skucode] int,[ordcom] varchar(12),[skupch] varchar(8),[qty] int)
insert [发货表]
select 185469,'科技有限公司',NULL,31 union all
select 570798,'科技有限公司','SA9316DW',5
--> 测试数据:[库存表]
if object_id('[库存表]') is not null drop table [库存表]
go 
create table [库存表]([skucode] int,[recname] varchar(12),[skupch] varchar(10),[qty] int)
  相关解决方案