文章目录
- 金蝶云星空历史库存信息批量计算生成
-
- 业务背景
- 需求背景
- 参考
- 开发步骤
-
- 创建单据《历史库存信息》
-
- 界面
- 表结构
- 需要生成日期库存信息的日期范围存储为表
-
- 创建日期表
- 使用递归插入数据
- 创建存储过程1,根据日期生成计算当日库存,生成《历史库存信息》
- 创建存储过程2,根据日期范围循环调用存储过程1,生成《历史库存信息》
- 执行存储过程生成
- 查询数据
金蝶云星空历史库存信息批量计算生成
业务背景
今天是2024年07月30日,系统2024年01月01日启用,导入初始库存。
需求背景
需要快速查询库存组织=供应链中心下,某仓库某物料的库存数。后面还需要按照过去时间范围查询每一天的库存量以监控变化。
参考
《库存汇总表》《库存余额》《库存账龄分析》《物料收发明细表》
开发步骤
创建单据《历史库存信息》
字段:日期,单据编号,数据状态,库存组织,仓库,物料编码,库存数,锁库数
界面
表结构
CREATE TABLE [XXXX_T_STK_HisInventory](
[FID] [BIGINT] NOT NULL PRIMARY KEY,
[FBILLNO] [NVARCHAR](30) NOT NULL,
[FDOCUMENTSTATUS] [CHAR](1) NOT NULL,
[F_XXXX_DATE] [DATETIME] NULL,
[F_XXXX_CREATEDATE] [DATETIME] NULL,
[F_XXXX_CREATORID] [INT] NOT NULL,
[F_XXXX_APPROVEDATE] [DATETIME] NULL,
[F_XXXX_MODIFYDATE] [DATETIME] NULL,
[F_XXXX_MODIFIERID] [INT] NOT NULL,
[F_XXXX_APPROVERID] [INT] NOT NULL,
[F_XXXX_STOCKORGID] [INT] NOT NULL,
[F_XXXX_STOCKID] [INT] NOT NULL,
[F_XXXX_STOCKQTY] [DECIMAL](23, 10) NOT NULL,
[F_XXXX_SYSLOCKQTY] [DECIMAL](23, 10) NOT NULL,
[F_XXXX_MATERIALID] [INT] NOT NULL)
需要生成日期库存信息的日期范围存储为表
后台生成2024-01-01至2024-07-21之间的数据。
创建日期表
CREATE TABLE date_tables2 (
date_column DATE PRIMARY KEY,--库存日期
begindate DATE,--关账后第一天
enddate DATE--库存日期+1
);
使用递归插入数据
;WITH DateRange AS
(
SELECT '2024-01-01' AS DateValue
UNION ALL
SELECT CONVERT(VARCHAR(10),DATEADD(Day,1,DateValue) ,23) AS DateValue
FROM DateRange
WHERE DateValue<='2024-07-20'
)
INSERT INTO date_tables2
SELECT DateValue
,CONVERT(VARCHAR(10)
,dateadd(day,-day(DateValue)+1,DateValue),120)
,DATEADD(DAY,1,DateValue)
FROM DateRange OPTION(MAXRECURSION 0)
注意如果报错“语句被终止。完成执行语句前已用完最大递归 100。”
解决方案 是 添加 OPTION(MAXRECURSION 0)
创建存储过程1,根据日期生成计算当日库存,生成《历史库存信息》
CREATE PROCEDURE [dbo].[XXXX_PR_STK_STOCKQTYALLGENERALINIT](
@DTIME VARCHAR(20),
@BEGINTIME VARCHAR(20),
@ENDTIME VARCHAR(20)
)
AS
BEGIN
DECLARE @BALTYPE INT
SET @BALTYPE=0
--获取当前关账的最后日期
DECLARE @fclosedate1 VARCHAR(10);
SET @fclosedate1=CONVERT(VARCHAR(10), DATEADD(D,-1,@BEGINTIME),23)
--PRINT '@fclosedate1:'+@fclosedate1
--SELECT CONVERT(VARCHAR(10), DATEADD(D,-1,'2024-01-01'),23)
IF(@BEGINTIME='2024-01-01')
BEGIN
SET @fclosedate1=@BEGINTIME
SET @BALTYPE=1
END
DECLARE @DATE DATETIME
SET @DATE=CAST(@DTIME AS DATETIME)
SET @BEGINTIME=@BEGINTIME+' 00:00:00'
SET @ENDTIME=@ENDTIME+' 00:00:00'
PRINT @BEGINTIME
PRINT @ENDTIME
--SELECT LEN('2024-06-01 00:00:00')
--DECLARE @BEGINTIME VARCHAR(20)
--SET @BEGINTIME='2024-06-01 00:00:00'
--DECLARE @ENDTIME VARCHAR(20)
--SET @ENDTIME='2024-06-03 00:00:00'
--DECLARE @STOCKID INT
--SET @STOCKID=493513
--获取当前关账最后日期
--DECLARE @fclosedate1 VARCHAR(10);
--SET @fclosedate1=(SELECT CONVERT(VARCHAR(10), MAX(FCLOSEDATE),23) fclosedate
--FROM T_STK_CLOSEPROFILE WHERE ((FORGID = (100006) AND FCATEGORY = 'STK')
--AND (FCLOSEDATE < '2024-06-01 00:00:00')) GROUP BY FORGID)
--PRINT '@fclosedate1:'+ @fclosedate1
SELECT m.fmaterialid,m.fstockid,SUM(m.fqtyaddoption*m.fbaseqty) fqty
INTO #m
FROM (
SELECT 'SAL_OUTSTOCK' fformid
, -1 fqtyaddoption
, t2.FMATERIALID fmaterialid
,t2.FSTOCKID
, t2.FBASEUNITQTY fbaseqty
FROM T_SAL_OUTSTOCK t0
LEFT OUTER JOIN T_SAL_OUTSTOCKFIN t1 ON t0.FID = t1.FID
LEFT OUTER JOIN T_SAL_OUTSTOCKENTRY t2 ON t0.FID = t2.FID
--LEFT OUTER JOIN t_BD_Stock st28 ON t2.FSTOCKID = st28.FStockId
LEFT OUTER JOIN t_BD_MaterialBase st238 ON t2.FMATERIALID = st238.FMATERIALID
LEFT OUTER JOIN t_BD_MaterialBase st239 ON t2.FMATERIALID = st239.FMATERIALID
WHERE (((((((t0.FDATE >= @BEGINTIME) AND (t0.FDATE < @ENDTIME))
AND t0.FSTOCKORGID = 100006) AND ((t1.FISGENFORIOS = '0' AND (st238.FERPCLSID <> '6'))
AND (st239.FSUITE <> '1'))) AND t0.FOBJECTTYPEID = 'SAL_OUTSTOCK') AND t0.FCANCELSTATUS = 'A')
--AND t2.FSTOCKID=@STOCKID
--AND ((st28.FNUMBER >= '02') AND (st28.FNUMBER <= '02'))
)
UNION ALL
SELECT 'SAL_RETURNSTOCK' fformid, 1 fqtyaddoption
, t1.FMATERIALID fmaterialid
, t1.FSTOCKID fstockid
, t1.FBASEUNITQTY fbaseqty
FROM T_SAL_RETURNSTOCK t0 LEFT OUTER JOIN T_SAL_RETURNSTOCKENTRY t1 ON t0.FID = t1.FID
LEFT OUTER JOIN T_SAL_RETURNSTOCKFIN t2 ON t0.FID = t2.FID
--LEFT OUTER JOIN T_BD_MATERIAL st11 ON t1.FMATERIALID = st11.FMATERIALID
--LEFT OUTER JOIN t_BD_Stock st17 ON t1.FSTOCKID = st17.FStockId
LEFT OUTER JOIN t_BD_MaterialBase st129 ON t1.FMATERIALID = st129.FMATERIALID
LEFT OUTER JOIN t_BD_MaterialBase st130 ON t1.FMATERIALID = st130.FMATERIALID
WHERE (((((((t0.FDATE >= @BEGINTIME) AND (t0.FDATE < @ENDTIME))
AND t0.FSTOCKORGID = 100006) AND (((t2.FISGENFORIOS = '0' AND (st129.FERPCLSID <> '6'))
AND (st130.FSUITE <> '1')) AND (t1.FRETURNTYPE <> '0fa6270ab70b416cb2a7141a8f182d64')))
AND t0.FOBJECTTYPEID = 'SAL_RETURNSTOCK') AND t0.FCANCELSTATUS = 'A')
--AND t1.FSTOCKID=@STOCKID
--AND t1.FMATERIALID IN (SELECT FMATERIALID FROM dbo.T_BD_MATERIAL WHERE fnumber=@MATERNUMBER)
--AND t1.FMATERIALID=669072
--AND ((((st11.FNUMBER >= '015102042') AND (st11.FNUMBER <= '015102042'))
--AND (st17.FNUMBER >= '02')) AND (st17.FNUMBER <= '02')))
)
UNION ALL
SELECT 'STK_InStock' fformid
, 1 fqtyaddoption --1 入库 0出库
, t2.FMATERIALID fmaterialid
,t2.FSTOCKID
,t2.FBASEUNITQTY fbaseqty
FROM t_STK_InStock t0 LEFT OUTER JOIN t_STK_InStockFin t1 ON t0.FID = t1.FID
LEFT OUTER JOIN T_STK_INSTOCKENTRY t2 ON t0.FID = t2.FID
--LEFT OUTER JOIN t_BD_Stock st24 ON t2.FSTOCKID = st24.FStockId
LEFT OUTER JOIN t_BD_MaterialBase st241 ON t2.FMATERIALID = st241.FMATERIALID
WHERE (((((((t0.FDATE >= @BEGINTIME) AND (t0.FDATE < @ENDTIME))
AND t0.FSTOCKORGID = 100006)
AND (t1.FISGENFORIOS = '0' AND st241.FISINVENTORY = 1))
AND t0.FOBJECTTYPEID = 'STK_InStock')
AND t0.FCANCELSTATUS = 'A')
--AND t2.FSTOCKID=@STOCKID
--AND ((st24.FNUMBER >= '02')
--AND (st24.FNUMBER <= '02'))
)
UNION ALL
SELECT 'STK_InStock' fformid
, -1 fqtyaddoption
, t2.FMATERIALID fmaterialid
,t2.FRECEIVESTOCKID fstockid
, t2.FBASEUNITQTY fbaseqty
FROM t_STK_InStock t0
LEFT OUTER JOIN t_STK_InStockFin t1