實現庫房批次管理,先進先出原則(一次難忘的找bug經歷)
阿新 • • 發佈:2019-01-10
新加一個物資臺賬功能。
上午設計表結構,下午測試資料。
原則是保證物資清關的時候,一個PO單據可以分批次收實現批次管理功能,而且發貨要保證先進先出的原則。
當天下午開始寫儲存過程,邏輯挺簡單的:
ALTER PROCEDURE [dbo].[SP_INV_SelectWarehouseAccount]
@dbname nvarchar(100),
@Receive_sn nvarchar(100),
@ischs char(1),
@error nvarchar(max) output
AS
BEGIN TRANSACTION
------------------------------------------------------------------------------------------------------------------------
----------- 收料單插入物資臺賬 -----------
------------------------------------------------------------------------------------------------------------------------
DECLARE
@PO_SN as nvarchar(100),
@Batch_No as nvarchar(2) ='00',
@Item_Code nvarchar(100);
----通過Receiving_SN查詢到po單號 (前臺只能選一個po單 獲取一個就可以了)
select top(1) @PO_SN = PO_Code from dbo.INV_Receivedet where [email protected] and [email protected]_sn;--REC1810018
select @Batch_No = MAX(Batch_No), @Item_Code = Item_Code
from INV_WarehouseAccount
where PO_SN in(select distinct po_sn from dbo.INV_Receivedet where [email protected] and [email protected]_sn)
and Item_Code in(select Material_Code from dbo.INV_Receivedet where [email protected] and [email protected]_sn)
group by Item_Code;
--判斷庫房臺賬是否有此單據的物資,如果沒此PO單據的話執行語句1,否則2
IF (@Batch_No='00')
--1
BEGIN
insert into INV_WarehouseAccount
(DB_Center,PO_SN,Item_Code,Batch_No,Warehouse_SN,Bin_SN,UOM_SN,Dept_SN,Ini_Qty,Receive_Qty,Issue_Qty,Return_Qty,INV_Balance_Qty,Check_Qty,Price,USD_Price,Status)
SELECT
A.DB_Center,A.PO_Code,A.Material_Code AS ITEM_CODE, 1 Batch_No,B.Warehouse_SN,C.Bin_Code,a.Convert_UOM,
(select dept.dept_sn from dbo.BAS_Department dept,BAS_Employee emp,dbo.INV_Receive det where dept.Dept_SN=emp.Dept_SN and emp.Employee_Name=det.Receiving_By and det.Receiving_SN=a.Receiving_SN)dept_sn,
0 Ini_Qty,A.Quantity receive_Qty,0 issue_Qty,0 return_Qty,0+A.Quantity INV_Balance_Qty,0 Check_Qty,
A.Unit_Price, A.Unit_Price*5.8 USD_price,'O'status
FROM dbo.INV_ReceiveDet A,dbo.BAS_Warehouse B,dbo.INV_Bin C
WHERE A.DB_Center=B.DB_Center
AND A.DB_Center=C.DB_Center
AND A.Bin_SN=C.Bin_SN
AND C.Warehouse_SN=B.Warehouse_SN
AND exists(select 1 from dbo.INV_Receive det where a.Receiving_SN=det.Receiving_SN AND [email protected]_SN)
and [email protected]_sn
ORDER BY A.Receiving_SN;
END
ELSE
--2
insert into dbo.INV_WarehouseAccount_temp --插入臨時表給同一單號有不同物資不同時收只用max batchno會有問題,下邊採取臨時表插入
select max(Batch_No), Item_Code
from INV_WarehouseAccount
where PO_SN in(select distinct po_sn from dbo.INV_Receivedet where [email protected] and [email protected]_sn)
and Item_Code in(select Material_Code from dbo.INV_Receivedet where [email protected] and [email protected]_sn)
group by Item_Code;
--DECLARE @i as int =1,
-- @countNum as int;
--select @countNum = count(*) from INV_WarehouseAccount_temp;
--WHILE @i<[email protected]
BEGIN
insert into INV_WarehouseAccount
SELECT
A.DB_Center,A.PO_Code,A.Material_Code AS ITEM_CODE, (SELECT TT.BATCH_NO+1 FROM INV_WarehouseAccount_temp TT WHERE TT.ITEM_CODE=A.Material_Code) Batch_No,B.Warehouse_SN,C.Bin_Code,a.Convert_UOM,
(select dept.dept_sn from dbo.BAS_Department dept,BAS_Employee emp,dbo.INV_Receive det where dept.Dept_SN=emp.Dept_SN and emp.Employee_Name=det.Receiving_By and det.Receiving_SN=a.Receiving_SN)dept_sn,
0 Ini_Qty,A.Quantity receive_Qty,0 issue_Qty,0 return_Qty,0+A.Quantity INV_Balance_Qty,0 Check_Qty,
A.Unit_Price, A.Unit_Price*5.8 USD_price,'O'status
FROM dbo.INV_ReceiveDet A,dbo.BAS_Warehouse B,dbo.INV_Bin C
WHERE A.DB_Center=B.DB_Center
AND A.DB_Center=C.DB_Center
AND A.Bin_SN=C.Bin_SN
AND C.Warehouse_SN=B.Warehouse_SN
AND exists(select 1 from dbo.INV_Receive det where a.Receiving_SN=det.Receiving_SN AND [email protected]_SN)
and [email protected]_sn
and a.Material_Code in(select item_code from INV_WarehouseAccount_temp aa where a.Material_Code = AA.ITEM_CODE)
ORDER BY A.Receiving_SN;
--清除臨時表
delete from INV_WarehouseAccount_temp;
END
UPDATE T
SET T.Batch_No =
CASE
WHEN T.Batch_No=1 THEN '01'
WHEN T.Batch_No=2 THEN '02'
WHEN T.Batch_No=3 THEN '03'
WHEN T.Batch_No=4 THEN '04'
WHEN T.Batch_No=5 THEN '05'
WHEN T.Batch_No=6 THEN '06'
WHEN T.Batch_No=7 THEN '07'
WHEN T.Batch_No=8 THEN '08'
WHEN T.Batch_No=9 THEN '09'
END
FROM INV_WarehouseAccount T
WHERE T.PO_SN [email protected]_SN;
COMMIT TRANSACTION;
BEGIN TRANSACTION
if @@ERROR=0
begin
set @error=''
commit
end
else
begin
if @ischs='Y'
set @error='儲存失敗,請聯絡系統管理員'
if @ischs='N'
set @error='Fail To Save, Please Contact Administrator!'
rollback
end
GO
嗯看起來不錯,是分批次了。 至於先進先出,還沒寫完,下週。。。再說吧。週末開開心心玩去了~
最終的結果是,儲存過程寫的很快,在資料庫執行就沒問題,一經過程式呼叫就沒有插入到我的主表裡。。。
下邊這個圖是正確且可以呼叫的的:
檢查了好幾遍也沒有看出什麼錯誤,依舊是程式執行資料存不到主表裡,資料庫執行就可以。
萬般無奈,只好借用SQL SERVER 自帶抓指令碼神器:SQL Server Profiler
正確開啟方式為:
具體用法就不多說了。
看最終結果。擷取到的語句。
receivesn 中間少了個_ 我一口老血,噗。。。。
儲存過程寫的時候沒有_ 在pb程式裡也直接複製過去了,然後手賤加了個_編譯通過。。。
而且PB程式還不報錯!!!最終敗給了PB
結論:
1.SQL Server Profiler是個好東西值鼓勵大家都學會去使用
2.改好的東西千萬隨便不要動 改動要。。。加註釋說明一下。作死只會讓自己死得更快。