I’ve created a pseudo code in my Sybase database to store bulk transactions in the table. However, it’s only saving half of them before stopping.
Upon rerunning it, I encounter truncating errors as half of the transactions are already stored. Despite implementing BEGIN ATOMIC to revert changes, the halting problem persists.
The code is as below:
ALTER PROCEDURE "DBA"."CW_UpdateNegCharges" (IN @ReferenceDate DATE)
BEGIN
DECLARE @InvoiceConfigHeader Entity_No;
DECLARE @LocalSiteNo Entity_No;
DECLARE @AuditDescription VARCHAR(1000);
DECLARE @JobList VARCHAR(900);
DECLARE @CustList VARCHAR(900);
DECLARE @GotDate DATETIME;
DECLARE @ComputerName Windows_Computer_Name;
SET @ComputerName = PROPERTY('MachineName');
SET @GotDate = current utc timestamp;
SELECT SM2_GetLocalSiteNo() INTO @LocalSiteNo;
SET @AuditDescription = STRING('Started Job and Customer Negotiated Charge Update for Date ', @ReferenceDate, ' On ', @GotDate);
INSERT INTO SM2_Audit_Log(Site_No, Application_Name, Originator,
Event_ID, Event_Class, Event_Severity, Event_Description, Operator_Code, Replication_Group_ID, Windows_Computer_Name)
VALUES(@LocalSiteNo, 'Database', 'Negotiated Charge Effective Date',
7302, 0, 10, @AuditDescription, CURRENT USER, RTRIM(CAST(@LocalSiteNo AS CHAR)), @ComputerName);
--Gets list of Jobs updated
WHILE EXISTS(SELECT 1 FROM SM2_Job_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL) LOOP
UPDATE SM2_Job_Product_Auth_Tran
SET Neg_Unit_Cost_Override = Neg_Unit_Cost_Override_New,
Neg_Min_Charge_Override = Neg_Min_Charge_Override_New,
Neg_Unit_Cost_Override_Cost = Neg_Unit_Cost_Override_Cost_New,
Neg_Min_Charge_Override_Cost = Neg_Min_Charge_Override_Cost_New
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
AND Job_No = (SELECT TOP 1 Job_No FROM SM2_Job_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
ORDER BY Job_No);
SELECT TOP 1 STRING(COALESCE(@JobList + ',', ''), Job_No) INTO @JobList
FROM SM2_Job_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
ORDER BY Job_No;
UPDATE SM2_Job_Product_Auth_Tran
SET Neg_Unit_Cost_Override_New = NULL,
Neg_Min_Charge_Override_New = NULL,
Neg_Unit_Cost_Override_Cost_New = NULL,
Neg_Min_Charge_Override_Cost_New = NULL,
New_Charge_Start_Date = NULL
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
AND Job_No = (SELECT TOP 1 Job_No FROM SM2_Job_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
ORDER BY Job_No);
END LOOP;
--Gets list of Customers updated
WHILE EXISTS(SELECT 1 FROM SM2_Customer_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL) LOOP
UPDATE SM2_Customer_Product_Auth_Tran
SET Neg_Unit_Cost_Override = Neg_Unit_Cost_Override_New,
Neg_Min_Charge_Override = Neg_Min_Charge_Override_New
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
AND Customer_No = (SELECT TOP 1 Customer_No FROM SM2_Customer_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
ORDER BY Customer_No);
SELECT TOP 1 STRING(COALESCE(@CustList + ',', ''), Customer_No) INTO @CustList
FROM SM2_Customer_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
ORDER BY Customer_No;
UPDATE SM2_Customer_Product_Auth_Tran
SET Neg_Unit_Cost_Override_New = NULL,
Neg_Min_Charge_Override_New = NULL,
New_Charge_Start_Date = NULL
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
AND Customer_No = (SELECT TOP 1 Customer_No FROM SM2_Customer_Product_Auth_Tran
WHERE New_Charge_Start_Date <= @ReferenceDate
AND New_Charge_Start_Date IS NOT NULL
ORDER BY Customer_No);
END LOOP;
SET @AuditDescription = STRING('Negotiated Charge Update for ',@ReferenceDate, ' complete.');
IF @JobList IS NULL THEN
SET @AuditDescription = STRING(@AuditDescription,' No Jobs updated.');
ELSE
SET @AuditDescription = STRING(@AuditDescription,' Jobs Updated: ', @JobList);
END IF;
IF @CustList IS NULL THEN
SET @AuditDescription = STRING(@AuditDescription,' No Customers updated.');
ELSE
SET @AuditDescription = STRING(@AuditDescription,' Customers Updated: ', @CustList);
END IF;
INSERT INTO SM2_Audit_Log(Site_No, Application_Name, Originator,
Event_ID, Event_Class, Event_Severity, Event_Description, Operator_Code, Replication_Group_ID, Windows_Computer_Name)
VALUES(@LocalSiteNo, 'Database', 'Negotiated Charge Effective Date',
7302, 0, 10, @AuditDescription, CURRENT USER, RTRIM(CAST(@LocalSiteNo AS CHAR)), @ComputerName);
END
How can I adjust it to execute all transactions or resume from the last saved one upon restarting?
syed Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.