日期:2014-05-17 浏览次数:20505 次
nextinsert:
--插入月结主表对应数据
insert into dbo.t_StockStorage
( c_ID ,
c_Type ,
c_JigouID ,
c_Month ,
c_StaffID ,
c_Ctime ,
c_Remark
)
VALUES ( @storid, -- c_ID - nvarchar(50)
'无毛绒' , -- c_Type - nvarchar(50)
@JigouID , -- c_JigouID - nvarchar(50)
@yjtime, -- c_Month - datetime
@staffid , -- c_StaffID - nvarchar(50)
@createtime , -- c_Ctime - datetime
@remark -- c_Remark - ntext
)
--获取插入到月结主表最新的主键id
select @storageId = scope_identity()
/*计算库存月结数据逻辑SQL语句,可以利用临时表或with cte的用法,下文以cte为主。*/
--向月结子表插入数据
--insert into StockStorageDetail
--select @storageId,productid,quantity
--from cte
IF EXISTS(SELECT 1 FROM dbo.t_StockStorage WHERE c_JigouID=@JigouID)
/*第一种情况:从未做过月结*/
BEGIN
SELECT * INTO #t_inv FROM dbo.t_CashmereInventory WHERE c_Pihao IN (SELECT c_Pihao FROM dbo.v_CashmereInBodyDetails WHERE c_time<=@yjtime AND c_jigouID=@JigouID AND IsAccount=1)
UPDATE #t_inv SET c_Inventory=0 --将数据归零准备重新计算
DECLARE yb_ph CURSOR FOR
SELECT c_Pihao,c_ChangduID,c_PositionID FROM #t_inv WHERE c_JiGouID=@JigouID;
SELECT @count = count(*) FROM #t_inv WHERE c_JiGouID=@jigouid;
OPEN yb_ph
WHILE @count >0 AND @@FETCH_STATUS=0
BEGIN
FETCH NEXT FROM yb_ph INTO @pihao,@changdu,@kuwei
/*先取入库数据进行运算*/
SELECT @rksj =c_Weight FROM dbo.v_CashmereInBodyDetails WHERE c_Pihao=@pihao AND LengthID=@changdu AND c_PositionID=@kuwei AND c_jigouID=@JigouID
UPDATE #t_inv SET c_Inventory=c_Inventory+@rksj WHERE c_Pihao=@pihao AND c_ChangduID=@changdu AND c_PositionID=@kuwei AND c_jigouID=@JigouID
/*再取出库数据进行运算*/
SELECT @cksj =c_Weight FROM dbo.v_CashmereOutDetails WHERE c_Pihao=@pihao AND LenthID=@changdu AND c_PositionID=@kuwei AN