日期:2014-05-17  浏览次数:20606 次

判断库存是数量是否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