Good afternoon everyone. As I am relatively new to the SAP Community please overlook any misstatements I might make. I am currently running the SAP B1 HANA version. Coming from SQL this is quite a change for me, but not too bad. The issue I am currently having is with a stored procedure. As we add more and more data this has gotten slower and slower with each passing day. I am hoping someone can recommend a fix to the problem or spot a mistake I might have made. Here is the SP.
CREATE PROCEDURE "TWBS_SP_ShipEasy_SetQtyBoxWise" (IN DocEntry nvarchar(255)) AS
U_UPSTrackNum nvarchar(50);
U_UPSBoxNum nvarchar(10);
U_SAPDocNum nvarchar(10);
U_ObjType nvarchar(10);
U_ShipType nvarchar(10);
U_BoxWeight nvarchar(30);
U_PackType nvarchar(30);
U_PackName nvarchar(30);
U_Length nvarchar(30);
U_Width nvarchar(30);
U_Height nvarchar(30);
U_Processed nvarchar(30);
U_CODAmt nvarchar(30);
U_UPSPackType nvarchar(30);
U_TWBS_CART_PEICE_CT nvarchar(30);
ItemCode nvarchar(30);
U_TWBS_UCC_CODE nvarchar(30);
TempCode integer;
TempCode_Line integer;
LineNum integer;
CURSOR mycursor123 FOR
SELECT F."BoxNumber", "DocNum", "ObjType",
"sWeight1", "SLength1", "SWidth1", "SHeight1", "CarrierCode" AS "U_ShipType", 0 AS "U_Processed",
'Box' AS "U_PackName", 27 AS "U_PackType", 0 AS "U_CODAmt", '00' AS "U_UPSPackType", "U_TWBS_CART_PEICE_CT",
"ItemCode", a."SSCC", "LineNum"
FROM ( SELECT c1."SSCC", a."DocNum", a."ObjType", b."ItemCode", c."SWidth1", "SLength1",
IFNULL(b."U_TWBS_BOX_WEIGHT", 0) AS "sWeight1", "SHeight1",
ROW_NUMBER() OVER (PARTITION BY c1."InternalKey" ORDER BY c1."SSCC" DESC) AS "rown",
b."LineNum", T3."CarrierCode", c1.DeliveredQuantity as "U_TWBS_CART_PEICE_CT"
FROM
(
SELECT
PMX_LUID.SSCC,
PMX_INVD."DocLineNum",
PMX_INVD."InternalKey",
ODLN."DocEntry",
ODLN."DocNum" AS DeliveryDocNum,
ORDR."ObjType" AS DocType,
ORDR."DocNum" AS DocNum,
DLN1."LineNum" as LineNum,
DLN1."ItemCode",
DLN1."Dscription",
SUM(PMX_INVD."Quantity") * - 1 AS DeliveredQuantity
FROM DLN1
INNER JOIN ODLN ON DLN1."DocEntry" = ODLN."DocEntry"
INNER JOIN PMX_INVD ON ODLN."ObjType" = PMX_INVD."TransType"
AND DLN1."DocEntry" = PMX_INVD."DocEntry"
AND DLN1."LineNum" = PMX_INVD."DocLineNum"
INNER JOIN PMX_LUID ON PMX_INVD."LogUnitIdentKey" = PMX_LUID."InternalKey"
LEFT JOIN PMX_ITRI ON PMX_ITRI."InternalKey" = PMX_INVD."ItemTransactionalInfoKey"
LEFT JOIN ORDR
INNER JOIN RDR1 ON ORDR."DocEntry" = RDR1."DocEntry" ON DLN1."BaseType" = ORDR."ObjType"
AND DLN1."BaseEntry" = RDR1."DocEntry"
AND DLN1."BaseLine" = RDR1."LineNum"
WHERE ODLN."DocEntry" = :DocEntry
GROUP BY PMX_LUID.SSCC,
PMX_INVD."DocLineNum",
PMX_INVD."InternalKey",
ODLN."DocEntry",
ODLN."DocNum",
ORDR."ObjType",
ORDR."DocNum",
DLN1."LineNum",
DLN1."ItemCode",
DLN1."Dscription",
RDR1."Quantity"
) AS c1
LEFT OUTER JOIN "DLN1" b ON c1."DocEntry" = b."DocEntry" AND c1."DocLineNum" = b."LineNum"
--AND c1."TransType" = '15'
LEFT OUTER JOIN "ODLN" a ON b."DocEntry" = a."DocEntry"
LEFT OUTER JOIN "OITM" c ON c1."ItemCode" = c."ItemCode"
-----------------------------------------------------------------------------------------------------
INNER JOIN "OCRD" T0 ON T0."CardCode" = a."CardCode" AND IFNULL(T0."U_TWBS_MASTER_PACK", 'N') = 'N'
-------------------------------------------------------------------------------------------------------
LEFT OUTER JOIN (SELECT T0.*, T1."CarrierCode" FROM "@TWBS_UPS_SHP" T0
INNER JOIN "TWBS_SBO-Common"."TWBS_UPS_SHP_Lookup" T1 ON T0."U_UPSShp" = T1."ID") AS T3
ON a."TrnspCode" = T3."Code" WHERE a."DocEntry" = :DocEntry ) AS a ,
( SELECT ROW_NUMBER() OVER ( ORDER BY "SSCC") AS "BoxNumber", "SSCC"
FROM (
select DISTINCT "SSCC"
FROM PMX_INVD
where "DocEntry" = :DocEntry and "TransType" = '15'
) AS R ) as F
WHERE "rown" = 1 and a."SSCC" = F."SSCC"
ORDER BY "LineNum";
BEGIN
---------------------------Header -----------------------
Delete From "SYSTEM"."SE_Box_table";
FOR curso_row as mycursor123 DO
U_UPSBoxNum:=curso_row."BoxNumber";
U_SAPDocNum:=curso_row."DocNum";
U_ObjType:=curso_row."ObjType";
U_BoxWeight:=curso_row."sWeight1";
U_Length:=curso_row."SLength1";
U_Width:=curso_row."SWidth1";
U_Height:=curso_row."SHeight1";
U_ShipType:=curso_row."U_ShipType";
U_Processed:=curso_row."U_Processed";
U_PackName:=curso_row."U_PackName";
U_PackType:=curso_row."U_PackType";
U_CODAmt:=curso_row."U_CODAmt";
U_UPSPackType:=curso_row."U_UPSPackType";
U_TWBS_CART_PEICE_CT:=curso_row."U_TWBS_CART_PEICE_CT";
ItemCode:=curso_row."ItemCode";
U_TWBS_UCC_CODE:=curso_row."SSCC";
LineNum :=curso_row."LineNum";
INSERT INTO "SYSTEM"."SE_Box_table" ("U_UPSBoxNum", "U_SAPDocNum", "U_ObjType", "U_BoxWeight", "U_Length",
"U_Width", "U_Height", "U_ShipType", "U_Processed", "U_PackName", "U_PackType", "U_CODAmt", "U_UPSPackType",
"U_TWBS_CART_PEICE_CT", "ItemCode", "U_TWBS_UCC_CODE", "LineNum")
VALUES (U_UPSBoxNum, U_SAPDocNum, U_ObjType, U_BoxWeight,
U_Length, U_Width, U_Height, U_ShipType, U_Processed, U_PackName, U_PackType, U_CODAmt,
U_UPSPackType, U_TWBS_CART_PEICE_CT, ItemCode, U_TWBS_UCC_CODE, LineNum);
end for;
DELETE FROM "@TWBS_UPS_HDR" WHERE "U_SAPDocNum" = (SELECT "DocNum" FROM ODLN WHERE "DocEntry" = :DocEntry);
DELETE FROM "@TWBS_UPS_LINES" WHERE "U_SAPDocNum" = (SELECT "DocNum" FROM ODLN WHERE "DocEntry" = :DocEntry);
SELECT (SELECT IFNULL(MAX(cast("Code" as integer)),0) FROM "@TWBS_UPS_HDR") INTO TempCode FROM DUMMY;
SELECT (SELECT IFNULL(MAX(cast("Code" as integer)),0) FROM "@TWBS_UPS_LINES") INTO TempCode_Line FROM DUMMY;
INSERT INTO "@TWBS_UPS_HDR" ("Code", "Name", "U_BoxNum", "U_SAPDocNum", "U_ObjType", "U_BoxWeight", "U_Length",
"U_Width", "U_Height", "U_ShipType", "U_Processed", "U_PackName", "U_PackType", "U_CODAmt", "U_UPSPackType",
"U_ShipRef1")
(SELECT (TempCode + ROW_NUMBER() OVER ( ORDER BY "U_SAPDocNum")) AS "code",
(TempCode + ROW_NUMBER() OVER ( ORDER BY r."U_SAPDocNum")) AS "Name", "U_UPSBoxNum", "U_SAPDocNum",
"U_ObjType", 0, 0, 0, 0, "U_ShipType", "U_Processed",
"U_PackName", "U_PackType", "U_CODAmt", "U_UPSPackType", "U_TWBS_UCC_CODE"
FROM ( SELECT DISTINCT "U_UPSBoxNum", "U_SAPDocNum",
"U_ObjType", 0, 0, 0, 0, "U_ShipType", "U_Processed",
"U_PackName", "U_PackType", "U_CODAmt", "U_UPSPackType", "U_TWBS_UCC_CODE"
FROM "SYSTEM"."SE_Box_table" ) as r
ORDER BY r."U_SAPDocNum");
INSERT INTO "@TWBS_UPS_LINES" ("Code", "Name", "U_UPSBoxNum", "U_SAPDocNum", "U_SAPItemCode",
"U_Quantity", "U_UnitWeight", "U_LineNum", "U_ObjType") (SELECT (TempCode_Line + ROW_NUMBER() OVER ( ORDER BY "U_SAPDocNum")) AS "code",
(TempCode_Line + ROW_NUMBER() OVER ( ORDER BY "U_SAPDocNum")) AS "Name", "U_UPSBoxNum", "U_SAPDocNum",
"ItemCode", "U_TWBS_CART_PEICE_CT", "U_BoxWeight", "LineNum" as "U_LineNum",15
FROM "SYSTEM"."SE_Box_table"
ORDER BY "U_UPSBoxNum");
END;
This is used to transfer data from one system over to a shipping station. 2 Different products. Appreciate any input on this.