Looping through multiple loops in - mysql

DROP PROCEDURE fillBin;
DELIMITER $$
CREATE PROCEDURE fillBin(IN a INT(11), IN b INT(11), IN c INT(11), IN d INT(11), IN e INT(11))
wholeblock:BEGIN
DECLARE i INT UNSIGNED DEFAULT 1;
DECLARE j INT UNSIGNED DEFAULT 1;
DECLARE k INT UNSIGNED DEFAULT 1;
DECLARE l INT UNSIGNED DEFAULT 1;
DECLARE m INT UNSIGNED DEFAULT 1;
DECLARE strA VARCHAR(255) DEFAULT '';
DECLARE strB VARCHAR(255) DEFAULT '';
DECLARE strC VARCHAR(255) DEFAULT '';
DECLARE strD VARCHAR(255) DEFAULT '';
DECLARE strE VARCHAR(255) DEFAULT '';
DECLARE strF VARCHAR(255) DEFAULT '';
SET strA = 'A';
SET strB = 'G';
SET strC = 'R';
SET strD = 'S';
SET strE = 'B';
SET strF= '';
WHILE i < (a+1) DO
SET strA=CONCAT(strA,i) ;
WHILE j < (b+1) DO
SET strB = CONCAT(strB,j);
WHILE k < (c+1) DO
SET strC =CONCAT(strC,k);
WHILE l < (d+1) DO
SET strD = CONCAT(strD,l);
WHILE m < (e+1) DO
SET strE = CONCAT(strE,m);
SET strF = CONCAT(strA, strB, strC, strD, strE);
INSERT INTO BIN (`aisle`, `room`, `rack`, `shelf`, `bin`, `barcode`, `warehouse_id`)
VALUES(strA,strB, strC, strD, strE, strF, 1);
SET m=m+1;
SET strE = 'B';
END WHILE;
SET l=l+1;
SET strD = 'S';
END WHILE;
SET k=k+1;
SET strC = 'R';
END WHILE;
SET j=j+1;
SET strB = 'G';
END WHILE;
SET i=i+1;
SET strA = 'A';
END WHILE;
END $$
//calling the procedure
CALL fillBin(1, 1, 1, 2, 2);
My procedure inserts the values: A1G1R1S1B1 and A1G1R1S1B2. And the inner loop breaks.
but the correct insertion should be: A1G1R1S1B1 and A1G1R1S1B2 and A1G1R1S2B1 and A1G1R1S2B2.
The insert is obviously incomplete. How should I place my insert statement or change the looping as to cater the needs?
Please help. Thanks

Why don't use simple SQL to do this task ?
Look at the following example
We need a table with a seqence of numbers from 1 to X - where X is a maximum number whenever we need.
CREATE TABLE numbers(
x int primary key auto_increment
);
INSERT INTO numbers
SELECT null FROM information_schema.columns;
SELECT max( x ) FROM numbers;
| MAX( X ) |
|----------|
| 558 |
And now the below simple select will generate all required records for us
(see this demo: http://sqlfiddle.com/#!2/1e5c4b/7
- you will need to scroll to the bottom of the page to see a result of the query):
SET #strA = 'A';
SET #strB = 'G';
SET #strC = 'R';
SET #strD = 'S';
SET #strE = 'B';
SET #strF= '';
SET #a = 2;
SET #b = 2;
SET #c = 2;
SET #d = 2;
SET #e = 2;
SELECT *,
CONCAT(strA, strB, strC, strD, strE) strF,
1 As warehouse_id
FROM (
SELECT Concat(#strA,x) strA
FROM numbers
WHERE x <= #a
) A
CROSS JOIN
(
SELECT Concat(#strB,x) strB
FROM numbers
WHERE x <= #b
) B
CROSS JOIN
(
SELECT Concat(#strC,x) strC
FROM numbers
WHERE x <= #c
) C
CROSS JOIN
(
SELECT Concat(#strD,x) strD
FROM numbers
WHERE x <= #d
) D
CROSS JOIN
(
SELECT Concat(#strE,x) strE
FROM numbers
WHERE x <= #e
) E
And now just place an INSERT statement at the top of the above query:
INSERT INTO BIN (`aisle`, `room`, `rack`, `shelf`, `bin`, `barcode`, `warehouse_id`)
SELECT *,
CONCAT(strA, strB, strC, strD, strE) strF,
1 As warehouse_id
FROM (
SELECT Concat(#strA,x) strA
FROM numbers
WHERE x <= #a
) A
CROSS JOIN
(
SELECT Co .....
........
........
........

Related

MySQL store procedure Assign value to multiple variable from select statement

This is my Stored procedure . I have problem to assign value to Declared variable . When I Execute it, Insert and Update Command work fine but VALUE of Declared Variable remain 0; But I have some value in Database. How can i Do this Corectly.
BEGIN
DECLARE PaidFee INT DEFAULT 0;
DECLARE DueFee INT DEFAULT 0;
DECLARE CourseFee INT DEFAULT 0;
INSERT INTO `creditdirectory`(`TypeID`, `PersonName`, `CreditBy`, `PersonID`, `ModeOfPayment`,`Details`,`Amount`,`CompanyID`)
VALUES(1,PersonName,CreditBy, AddmissionID, ModeOfPayment, 'Installment', PaidAmount,
CompanyID);
SELECT `CourseFee`,`PaidFee`,`DueFee` INTO CourseFee,PaidFee,DueFee FROM `studentcoursedetails` WHERE `ID`= CourseID;
SET PaidFee = PaidFee + PaidAmount;
SET DueFee = CourseFee - PaidFee;
IF (NextDueDate !='') THEN
UPDATE `studentcoursedetails` SET `PaidFee` = PaidFee, `DueFee` = DueFee, `DueDate` = NextDueDate WHERE `ID`= CourseID;
ELSE
UPDATE `studentcoursedetails` SET `PaidFee` = PaidFee, `DueFee` = DueFee, `DueDate` = NULL WHERE `ID` = CourseID;
END IF;
END
Don't use variables with same name of columns, the query will take preference on table column names.
A good idea is use variables with prefix:
BEGIN
DECLARE p_PaidFee INT DEFAULT 0;
DECLARE p_DueFee INT DEFAULT 0;
DECLARE p_CourseFee INT DEFAULT 0;
INSERT INTO `creditdirectory`(`TypeID`, `PersonName`, `CreditBy`, `PersonID`, `ModeOfPayment`,`Details`,`Amount`,`CompanyID`)
VALUES(1,PersonName,CreditBy, AddmissionID, ModeOfPayment, 'Installment', PaidAmount,
CompanyID);
SELECT `CourseFee`,`PaidFee`,`DueFee` INTO p_CourseFee,p_PaidFee,p_DueFee FROM `studentcoursedetails` WHERE `ID`= CourseID;
SET p_PaidFee = p_PaidFee + PaidAmount;
SET p_DueFee = p_CourseFee - p_PaidFee;
IF (NextDueDate !='') THEN
UPDATE `studentcoursedetails` SET `PaidFee` = p_PaidFee, `DueFee` = p_DueFee, `DueDate` = NextDueDate WHERE `ID`= CourseID;
ELSE
UPDATE `studentcoursedetails` SET `PaidFee` = p_PaidFee, `DueFee` = p_DueFee, `DueDate` = NULL WHERE `ID` = CourseID;
END IF;
END

SQL Error: No data - zero rows fetched, selected, or processed

CREATE PROCEDURE KC_update_pricing()
BEGIN
DECLARE u_sku VARCHAR(10) DEFAULT "";
DECLARE u_product_size VARCHAR(4) DEFAULT "";
DECLARE u_product_name VARCHAR(100) DEFAULT "";
DECLARE u_chargeble_area DECIMAL DEFAULT 0.0;
DECLARE u_user_id VARCHAR(10) DEFAULT "";
DECLARE u_last_update DATE;
DECLARE cur1 CURSOR FOR
SELECT
sku,
product_size,
product_name,
chargeble_area,
user_id,
last_update
FROM kcproduct_test_load
WHERE last_update >= (SELECT DATE_FORMAT(DATE_SUB(max(last_update), INTERVAL 1 DAY), '%d-%m-%Y')
FROM kcpricing_test_load);
OPEN Cur1;
LOOP
FETCH cur1
INTO u_sku, u_product_size, u_product_name, u_chargeble_area, u_user_id, u_last_update;
IF (u_product_size = 'S')
THEN
SET u_shipping = 99;
ELSEIF (u_product_size = 'M')
THEN
SET u_shipping = 149;
ELSEIF (u_product_size = 'L')
THEN
SET u_shipping = 199;
ELSEIF (u_product_size = 'XS')
THEN
SET u_shipping = 99;
ELSEIF (u_product_size = 'XXS')
THEN
SET u_shipping = 69;
ELSE
SET u_shipping = 199;
END IF;
UPDATE kcpricing_test_load
SET base_price = (u_chargeble_area * 150)
WHERE sku = u_SKU;
END LOOP;
END;
have an exception command in the cursor some thing like below :
Exception
When NO_DATA_FOUND then
continue
end;
it will help to avoid no data found error.

how to split and select value in row in mysql

CREATE DEFINER=`root`#`localhost` PROCEDURE `TestExampl2_SP`(in Array_Value varchar(255))
begin
declare i int default 0;
declare loopcount int default 0;
declare arrayChar varchar(50) ;
declare isexist int(10) default 0;
declare existString varchar(50);
declare notexistString varchar(50) ;
set loopcount=( select LENGTH(Array_Value) - LENGTH(REPLACE(Array_Value, ',', '')));
while i<=loopcount do
SET i = i + 1;
set arrayChar =(
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(Array_Value, ',', i), ',', -1));
set isexist=(select count(*) from emp
where ename=arrayChar);
if(isexist >0 ) then
select CONCAT_WS(',',existString,arrayChar) into existString;
else
select CONCAT_WS(',',notexistString,arrayChar) into notexistString;
end if;
END WHILE;
select notexistString;
END
This is my Procure when i execute this Procedure whith input call TestExampl2_SP('a,m,n,x,y,z') i am getting notexistString ='x,y,z' but insetd of this i want result row wise i.e i have to split by comma
like this :
**value**
x
y
z
please suggest me how i will implement this .
You can follow below code to split string in sql.
CREATE FUNCTION SplitString
(
#Input NVARCHAR(MAX),
#Character CHAR(1)
)
RETURNS #Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE #StartIndex INT, #EndIndex INT
SET #StartIndex = 1
IF SUBSTRING(#Input, LEN(#Input) - 1, LEN(#Input)) <> #Character
BEGIN
SET #Input = #Input + #Character
END
WHILE CHARINDEX(#Character, #Input) > 0
BEGIN
SET #EndIndex = CHARINDEX(#Character, #Input)
INSERT INTO #Output(Item)
SELECT SUBSTRING(#Input, #StartIndex, #EndIndex - 1)
SET #Input = SUBSTRING(#Input, #EndIndex + 1, LEN(#Input))
END
RETURN
END
And check using
SELECT Item FROM dbo.SplitString('Apple,Mango,Banana,Guava', ',')

Getting Error ''Incorrect arguments to EXECUTE'' while executing procedure in mysql

I am Getting Error
''Incorrect arguments to EXECUTE''
while executing procedure in mysql
My parameters are
CALL appclient.P_GetClientList( 1, 10, 1, ' AND -1!=0 AND isActive = 1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, 0,#V_TotalPages,#V_TotalRecords,#V_TabCountDetails);
DELIMITER $$
DROP PROCEDURE IF EXISTS `appclient.P_GetClientList`$$
CREATE PROCEDURE `appclient.P_GetClientList`(
IN V_ReportId INT,
IN V_PagePerRecordN INT,
IN V_PageN INT,
IN V_Condition VARCHAR (4000),
IN V_SortColIndex VARCHAR (10),
IN V_SortOrder VARCHAR (10),
IN V_Param1 VARCHAR (500),
IN V_Param2 VARCHAR (500),
IN V_Param3 VARCHAR (500),
IN V_Param4 VARCHAR (500),
IN V_Param5 VARCHAR (500),
IN V_Param6 VARCHAR (500),
IN V_Param7 VARCHAR (500),
IN V_Param8 VARCHAR (500),
IN V_Param9 VARCHAR (500),
IN V_Param10 VARCHAR (500),
IN V_IsPdf INT,
OUT V_TotalPages INT,
OUT V_TotalRecords INT,
OUT V_TabCountDetails VARCHAR (500)
)
BEGIN
DECLARE V_Query VARCHAR (4000) ;
DECLARE V_OrderBy NVARCHAR (500) ;
DECLARE V_ClientListId SMALLINT ;
DECLARE V_Logo NVARCHAR (50) ;
DECLARE V_Status VARCHAR (10) ;
DECLARE V_ClientId SMALLINT ;
DECLARE V_IsManDocsReq BIT ;
DECLARE V_IsApproveLink BIT ;
DECLARE V_ThemeId BIT ;
DECLARE V_IsActive BIT ;
DECLARE V_IsThemeLogoUploded BIT ;
DECLARE V_IsRegDocsUploaded BIT ;
DECLARE V_IsOnlinePayment BIT ;
DECLARE V_IsConfigurationDone BIT ;
DECLARE V_IsClientBidTermConfigured BIT ;
DECLARE V_IsClientBidTermGridLink BIT ;
DECLARE V_IsClientBidTermCreateLink BIT ;
DECLARE V_IsCPPPRequired TINYINT ;
DECLARE V_CPPPClientCount TINYINT ;
DECLARE V_State BOOLEAN DEFAULT FALSE;
IF(V_SortColIndex != '' AND V_SortColIndex IS NOT NULL AND V_SortOrder != '' AND V_SortOrder IS NOT NULL) THEN
SELECT V_OrderBy = CONCAT(RSCD.selectFieldName, ' ', V_SortOrder)
FROM `appreport.Tbl_ReportSearchColumnDetail` RSCD
WHERE RSCD.reportId = V_ReportId AND RSCD.columnId = V_SortColIndex;
END IF;
SET #V_Query = CONCAT('SELECT COUNT(1) into #V_TotalRecords FROM `appclient.tbl_Client` CM INNER JOIN `appclient.tbl_Department` DM ON CM.deptId = DM.deptId
WHERE 1 = 1 ',V_Condition);
SELECT #V_Query;
SELECT #V_TotalRecords;
PREPARE stm FROM #V_Query;
EXECUTE stm USING #V_TotalRecords;
DEALLOCATE PREPARE stm;
SET #V_Query1 = CONCAT('SELECT A.clientId, A.domainName, A.deptName, A.isManDocsReq, A.themeId, A.logo, A.isOnlinePayment,0 AS isApproveLink, 0 AS isClientBidTermCreateLink, 0 AS isClientBidTermGridLink, '''' AS cstatus, A.isActive,A.isCPPPRequired,A.cppCount
FROM (SELECT #row_number:=#row_number+1 AS row_number, CM.clientId, CM.domainName, DM.deptName,CM.isManDocsReq, CM.themeId, CM.logo, CM.isOnlinePayment, CM.isActive,CM.isCPPPRequired,CPC.cppCount
FROM `appclient.tbl_Client` CM
INNER JOIN `appclient.tbl_Department` DM ON CM.deptId = DM.deptId
LEFT OUTER JOIN (SELECT clientId,COUNT(clientId) cppCount FROM `appclient.tbl_ClientCPPPConfig` GROUP BY clientId) CPC ON CPC.clientId = CM.clientId,(SELECT #row_number:=0) AS t
WHERE 1 = 1', V_Condition,') AS A
WHERE ',#row_number, ' BETWEEN ' ,CAST((#V_PageN - 1) * #V_PagePerRecordN + 1 AS CHAR) ,' AND ' ,CAST(#V_PageN * #V_PagePerRecordN AS CHAR)) ;
SELECT #V_Query1;
/*INSERT INTO `ttbl_ClientList` (
clientId,domainName,deptName,isManDocsReq,themeId,logo,isOnlinePayment,isApproveLink,isClientBidTermCreateLink,isClientBidTermGridLink,cstatus,
isActive,isCPPPRequired,cppCount
)
*/
PREPARE stm FROM #V_Query1 ;
EXECUTE stm;
DEALLOCATE PREPARE stm; --
Loop_lable: WHILE (#V_Query1) DO
SET V_IsApproveLink = 0;
SET V_IsThemeLogoUploded = 0;
SET V_IsConfigurationDone = 0;
SET V_IsRegDocsUploaded = 0;
SET V_IsClientBidTermConfigured = 0;
SET V_IsClientBidTermGridLink = 0;
SET V_IsClientBidTermCreateLink = 0;
SET V_ClientId = 0;
SET V_IsCPPPRequired = 0;
SET V_CPPPClientCount = 0;
SELECT V_ClientId = clientId, V_IsManDocsReq = isManDocsReq, V_ThemeId = themeId, V_Logo = logo,
V_IsOnlinePayment = isOnlinePayment, V_IsActive = isActive, V_IsCPPPRequired = isCPPPRequired
FROM ttbl_ClientList WHERE clientListId = V_ClientListId;
/*
IF (#ROWCOUNT = 0) THEN
LEAVE Loop_lable;
END IF;
*/
IF (V_ThemeId != '' AND V_Logo != '' AND V_Logo != 'abc') THEN /* Theme and Logo */
SET V_IsThemeLogoUploded = 1;
END IF;
IF ((SELECT COUNT(1)
FROM appclient.tbl_ClientEventType CET INNER JOIN appmaster.tbl_Field MF ON CET.eventTypeId = MF.eventTypeId AND MF.isActive = 1
WHERE CET.clientId = V_ClientId AND CET.isActive = 1)
= (SELECT COUNT(1) FROM appclient.tbl_CustomParameter CP
INNER JOIN appclient.tbl_ClientEventType CET ON CP.eventTypeId = CET.eventTypeId AND CP.clientId=CET.clientId AND CET.isActive=1
WHERE CP.clientId = V_ClientId)) THEN /* Default Configuration */
SET V_IsConfigurationDone = 1;
END IF;
IF (V_IsManDocsReq = 1) THEN /* Bidder Registration Docs Required */
IF EXISTS (SELECT 1 FROM appclient.tbl_ClientRegDoc WHERE clientId = V_ClientId AND isActive = 1) THEN /* Mandatory Registration Docs Defined */
SET V_IsRegDocsUploaded = 1;
END IF;
ELSE
SET V_IsRegDocsUploaded = 1;
END IF;
/* Client bid term required */
IF ((SELECT COUNT(1)
FROM `appclient.tbl_ClientEventType` CET
LEFT OUTER JOIN `appclient.tbl_ClientBidTerm` CBT ON CET.clientId = CBT.clientId AND CET.eventTypeId = CBT.eventTypeId AND CBT.isActive = 1
WHERE CET.clientId = V_ClientId AND CET.isActive = 1 AND CBT.clientBidTermId IS NULL) = 0) THEN
SET V_IsClientBidTermGridLink = 1;
END IF;
IF ((SELECT COUNT(1)
FROM `appclient.tbl_ClientEventType` CET INNER JOIN appclient.tbl_ClientLanguage CL ON CET.clientId = CL.clientId AND CL.isActive = 1
WHERE CET.clientId = V_ClientId AND CET.isActive = 1)
= (SELECT COUNT(1) FROM `appclient.tbl_ClientBidTerm` CBT WHERE CBT.clientId = V_ClientId AND CBT.isActive = 1)) THEN
SET V_IsClientBidTermConfigured = 1;
ELSE
SET V_IsClientBidTermCreateLink = 1;
END IF;
IF (V_IsThemeLogoUploded = 1 AND V_IsConfigurationDone = 1 AND V_IsRegDocsUploaded = 1 AND V_IsClientBidTermConfigured = 1) THEN
SET V_IsApproveLink = 1;
END IF;
IF (V_IsActive = 1) THEN
SET V_IsApproveLink = 0;
SET V_Status = 'Approved';
ELSE
SET V_Status = 'Pending';
END IF;
UPDATE `ttbl_ClientList`
SET isApproveLink = V_IsApproveLink, isClientBidTermCreateLink = V_IsClientBidTermCreateLink,
isClientBidTermGridLink = V_IsClientBidTermGridLink, isOnlinePayment = V_IsOnlinePayment,
cstatus = V_Status
WHERE clientListId = V_ClientListId;
SET V_ClientListId = V_ClientListId + 1;
END WHILE;
SELECT clientId AS FieldValue1, CONCAT( domainName ,' </A> <BR>', deptName) AS FieldValue2,
deptName AS FieldValue3, isManDocsReq AS FieldValue4,CASE WHEN isCPPPRequired = 0 THEN isApproveLink WHEN (isCPPPRequired = 1 AND cppCount = 1) THEN isApproveLink ELSE 0 END AS FieldValue5, cstatus AS FieldValue6,
isClientBidTermCreateLink AS FieldValue7, isClientBidTermGridLink AS FieldValue8, isOnlinePayment AS FieldValue9,
CASE WHEN (isCPPPRequired = 1) THEN 1 ELSE 0 END AS FieldValue11
FROM `ttbl_ClientList`;
SET V_TotalPages = CEILING( V_TotalRecords / V_PagePerRecordN);
SET V_TabCountDetails = '';
END$$
DELIMITER ;

Mysql cursor fetches only first row

Mysql cursor fetches only first row and when it has fetched the second row the row_not_found variable is set to false and cursor close.
Please look into below SP:
CREATE DEFINER = 'root'#'localhost'
PROCEDURE billingv2test.SP_CreateRecurringBillingOrders(IN _billingDate DATETIME,
IN _defaultBillingFrequency INT,
IN _IsForcedExecution BIT)
BEGIN
DECLARE _userId char(36);
DECLARE _billingStartDate datetime;
DECLARE _billingEndDate datetime;
DECLARE _cmd VARCHAR(4000);
DECLARE _userBillingHistoryId char(36);
DECLARE _paymentOrderId char(36);
DECLARE _orderNumber VARCHAR(100);
DECLARE _totalChargeAmount DECIMAL(15, 6);
DECLARE _couponChargeAmount DECIMAL(15, 6);
DECLARE _pendingChargeAmount DECIMAL(15, 6);
DECLARE _isError BIT;
DECLARE _noOfUsersProcessed BIT;
DECLARE _billingResourceType VARCHAR(20);
DECLARE _RowNo INT;
DECLARE _defaultDateTime DATETIME;
DECLARE record_not_found INTEGER DEFAULT 0;
DECLARE user_list varchar(200);
DECLARE ProcessUsersForRecurringBilling_Cursor CURSOR FOR
SELECT OwnerId FROM UserBillingInfo
WHERE NextBillingDate IS NOT NULL
AND cast(NextBillingDate as date) <= cast( _billingDate as date)
AND IsProcessPending = 0
AND IsDeleted = 0
AND BillingStatus <> 'Delinquent'
ORDER BY NextBillingDate;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET record_not_found = 1;
SET _isError = 0;
SET _noOfUsersProcessed = 0;
SET _defaultDateTime = '1900-01-01 00:00:00';
SET _userBillingHistoryId = UUID();
INSERT INTO BillingHistory( Id, BillingStartTime, BillingEndTime, Status, NoOfUsersProcessed, CreateTime, UpdateTime )
VALUES ( _userBillingHistoryId, UTC_TIMESTAMP(), NULL , 'Started', 0, UTC_TIMESTAMP(), UTC_TIMESTAMP());
OPEN ProcessUsersForRecurringBilling_Cursor;
allusers: LOOP
FETCH ProcessUsersForRecurringBilling_Cursor INTO _userId;
IF record_not_found THEN
LEAVE allusers;
END IF;
SET user_list = CONCAT(IFNULL(user_list,''),", ",_userId);
SET _isError = 0;
SET _orderNumber = '';
SET _totalChargeAmount = '0';
SET _couponChargeAmount = '0';
SET _pendingChargeAmount = '0';
UPDATE UserBillingInfo SET IsProcessPending = 1 WHERE OwnerId = _userId;
SET _billingStartDate = _defaultDateTime;
SELECT
IFNULL(InvoiceDate, _defaultDateTime) INTO _billingStartDate
FROM
PaymentOrder
WHERE OwnerId = _userId AND OrderStatus IN ('Success', 'Submitted')
ORDER BY CreateTime DESC
LIMIT 1;
SELECT NextBillingDate INTO _billingEndDate FROM UserBillingInfo WHERE OwnerId = _userId;
SET _orderNumber = UUID();
SET _orderNumber = SUBSTRING(_orderNumber, 0, LOCATE('-', _orderNumber));
-- CALL SP_CreateRecurringBillingPaymentOrder
CALL SP_CreateRecurringBillingPaymentOrder
(_userId, _billingStartDate, _billingEndDate, _orderNumber, _userBillingHistoryId, _paymentOrderId);
SELECT Amount INTO _totalChargeAmount FROM PaymentOrder WHERE Id = _paymentOrderId;
SET _pendingChargeAmount = _totalChargeAmount;
UPDATE PaymentOrder set ChargeAmount = _pendingChargeAmount, UpdateTime = UTC_TIMESTAMP()
WHERE Id = _paymentOrderId;
UPDATE ResourceUsageProcessed SET BillingStatus = 'Completed'
WHERE PaymentOrderId = _paymentOrderId AND BillingStatus = 'Processing';
SET _noOfUsersProcessed = _noOfUsersProcessed + 1;
END LOOP allusers;
CLOSE ProcessUsersForRecurringBilling_Cursor;
UPDATE BillingHistory SET NoOfUsersProcessed = _noOfUsersProcessed, Status = 'Completed', BillingEndTime = UTC_TIMESTAMP()
WHERE Id = _userBillingHistoryId;
END
hey this may sound silly but try this
IF record_not_found=1 THEN
LEAVE allusers;