if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Cal_JieCun]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Cal_JieCun]
GOSET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
CREATE          PROCEDURE Cal_JieCun( @JCMonth char(7) ) 
as
--@JCMonth结存月
declare @intemp numeric(18,2)
declare @outtemp numeric(18,2)
declare @avgprice numeric(18,2)--COPY上月数据,本月期初数据=上月结存数据
insert into jiecuntemp(JieCunMonth,BomID,BomName,STANDARD,UNITS,QC_QTY,QC_AMOUNT,INQTY,OUTQTY,AVGPRICE,OUTAMOUNT,JIECUN,JIECUNAMOUNT)
select @JCMonth,bomid,bomname,standard,units,jiecun,jiecunamount,0,0,0,0,0,0
from StockJieCun where JieCunMonth=@JCMonth
--修改数据,入库,出库数据及入库金额,出库金额等update jiecuntemp set 
inqty=(select isnull(sum(qty),0) from Indelivery_d1 where convert(char(7),rkdate,120)=@JCMonth and status='2' and bomid=a.bomid ),   --本月入库数
outqty=(select isnull(sum(qty),0) from outdelivery_d1 where convert(char(7),ckdate,120)=@JCMonth and status='2' and bomid=a.bomid),
inamount=(select isnull(sum(amount),0) from Indelivery_d1 where convert(char(7),rkdate,120)=@JCMonth and status='2' and bomid=a.bomid),
/*平均单价*/
avgprice=(select case when QC_QTY=0 and inqty=0 then 0 else (QC_AMOUNT+INAMOUNT)/(QC_QTY+INQTY) end ),
/*出库金额*/
outamount=outqty*avgprice,
/*结存数*/
jiecun=qc_qty+inqty-outqty,
/*结存金额*/
jiecunamount=qc_amount+inamount-outamount
from jiecuntemp a--处理不在结存表中的新物料
// 下面的语句太长了我相精简一点。赋给变量现错!
--(select @intemp=Isnull(sum(qty),0) from indelivery_d1 where convert(char(7),rkdate ,120)=@JCMonth and status='2' and 
--bomid=a.bomxh) 这样也不行!
insert into jiecuntemp(JieCunMonth,BomID,BomName,STANDARD,UNITS,QC_QTY,QC_AMOUNT,INQTY,OUTQTY,AVGPRICE,OUTAMOUNT,JIECUN,JIECUNAMOUNT)
select @JCMonth,Bomxh,bomname,standard,unit,0,0,
--入库数量
(select Isnull(sum(qty),0) from indelivery_d1 where convert(char(7),rkdate ,120)=@JCMonth and status='2' and bomid=a.bomxh),  
--出库数量
(select Isnull(sum(qty),0) from outdelivery_d1 where convert(char(7),ckdate ,120)=@JCMonth and status='2' and bomid=a.bomxh),
--平均单价
(select Isnull(sum(amount),0)/Isnull(sum(qty),0) from indelivery_d1 where convert(char(7),rkdate,120)=@JCMonth and status='2' and bomid=a.bomxh),
--出库金额
(select Isnull(sum(amount),0)/Isnull(sum(qty),0) from indelivery_d1 where convert(char(7),rkdate,120)=@JCMonth and status='2' and bomid=a.bomxh)*
(select Isnull(sum(qty),0) from outdelivery_d1 where convert(char(7),ckdate ,120)=@JCMonth and status='2' and bomid=a.bomxh),
--结存数
(select Isnull(sum(qty),0) from indelivery_d1 where convert(char(7),rkdate ,120)=@JCMonth and status='2' and bomid=a.bomxh)-
(select Isnull(sum(qty),0) from outdelivery_d1 where convert(char(7),ckdate ,120)=@JCMonth and status='2' and bomid=a.bomxh),
--结存金额
(select sum(amount) from indelivery_d1 where convert(char(7),rkdate,120)=@JCMonth and status='2' and bomid=a.bomxh)-
(select Isnull(sum(amount),0)/Isnull(sum(qty),0) from indelivery_d1 where convert(char(7),rkdate,120)=@JCMonth and status='2' and bomid=a.bomxh)*
(select Isnull(sum(qty),0) from outdelivery_d1 where convert(char(7),ckdate ,120)=@JCMonth and status='2' and bomid=a.bomxh)
from bominfo a where bomxh not in( select bomid from jiecuntemp )
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO