error code 1048 column 'value' cannot be null - mysql
i have SP for update the data into new data with this query, so basically every id have own value for own rule_id, and this SP is to update the data with the new one from the SP
CREATE DEFINER=`root`#`localhost` PROCEDURE `update_discount_campaign_advanced2`(IN DiscountCampaignId INT(10),
IN rule_percentage VARCHAR(255),
IN rule_quota_daily_max VARCHAR(255),
IN rule_discount_daily VARCHAR(255),
IN rule_discount_weekly VARCHAR (255),
IN rule_discount_monthly VARCHAR(255),
IN rule_spesific_payment_method VARCHAR(255),
IN rule_spesific_cc_operator VARCHAR(255),
IN allow_other_product VARCHAR(255)
)
proc:
BEGIN
DECLARE is_discount_campaign_id INT(10);
DECLARE is_rule_percentage VARCHAR(255);
DECLARE is_rule_quota_daily_max VARCHAR(255);
DECLARE is_rule_discount_daily VARCHAR(255);
DECLARE is_rule_discount_weekly VARCHAR(255);
DECLARE is_rule_discount_monthly VARCHAR(255);
DECLARE is_rule_spesific_payment_method VARCHAR(255);
DECLARE is_rule_spesific_cc_operator VARCHAR(255);
DECLARE is_allow_other_product VARCHAR(255);
DECLARE param_1_message TEXT;
SET #is_discount_campaign_id = DiscountCampaignId;
SET #is_rule_percentage = rule_percentage;
SET #is_rule_quota_daily_max = rule_quota_daily_max;
SET #is_rule_discount_daily = rule_discount_daily;
SET #is_rule_discount_weekly = rule_discount_weekly;
SET #is_rule_discount_monthly = rule_discount_monthly;
SET #is_rule_spesific_payment_method = rule_spesific_payment_method;
SET #is_rule_spesific_cc_operator = rule_spesific_cc_operator;
SET #is_allow_other_product = allow_other_product;
SELECT CONVERT( CONCAT('Maaf, ID discount_campaign tidak bisa \'0\'') USING UTF8) INTO param_1_message;
IF (DiscountCampaignId = 0) THEN
SELECT param_1_message AS message;
LEAVE proc;
END IF;
IF (DiscountCampaignId != 0) THEN
INSERT INTO discount_campaign_advanced (id,discount_campaign_id,
discount_advanced_rules_id, value, createdAt, status, updatedAt)
select *, CASE WHEN
unitData.id is NULL THEN NULL ELSE NOW() END as updatedAt from (SELECT dca.id , DiscountCampaignId, dar.id as dar_id1, CASE WHEN dar.id = 1
THEN #is_rule_percentage WHEN dar.id = 2 THEN #is_rule_quota_daily_max
WHEN dar.id = 3 THEN #is_rule_discount_daily WHEN dar.id = 4
THEN #is_rule_discount_weekly WHEN dar.id = 5 THEN #is_rule_discount_monthly
WHEN dar.id = 6 THEN #is_rule_spesific_payment_method WHEN dar.id = 7
THEN #is_rule_spesific_cc_operator END, coalesce(dca.createdAt, NOW()), CASE when (dar.id = 1
and #is_rule_percentage != '0') THEN 1 WHEN (dar.id = 2 and
#is_rule_quota_daily_max != '0') THEN 1 WHEN
(dar.id = 3 and #is_rule_discount_daily != '0') THEN 1 WHEN (dar.id = 4 and
#is_rule_discount_weekly != '0') THEN 1
WHEN (dar.id = 5 AND #is_rule_discount_monthly != '0') THEN 1 WHEN (dar.id = 6
AND #is_rule_spesific_payment_method != '0')
THEN 1 WHEN (dar.id = 7 AND #is_rule_spesific_cc_operator != '0') THEN 1 ELSE 0 END as bb
FROM
discount_advanced_rules dar
LEFT JOIN
discount_campaign_advanced dca ON dca.discount_advanced_rules_id = dar.id
and dca.discount_campaign_id = DiscountCampaignId) as unitData
WHERE unitData.bb = 1
UNION
SELECT dca.id, DiscountCampaignId, dca.discount_advanced_rules_id, CASE WHEN dca.discount_advanced_rules_id = 1
THEN #is_rule_percentage WHEN dca.discount_advanced_rules_id = 2 THEN #is_rule_quota_daily_max
WHEN dca.discount_advanced_rules_id = 3 THEN #is_rule_discount_daily WHEN dca.discount_advanced_rules_id = 4
THEN #is_rule_discount_weekly WHEN dca.discount_advanced_rules_id = 5 THEN #is_rule_discount_monthly
WHEN dca.discount_advanced_rules_id = 6 THEN #is_rule_spesific_payment_method WHEN dca.discount_advanced_rules_id = 7
THEN #is_rule_spesific_cc_operator END, coalesce(dca.createdAt, NOW()), CASE when (dca.discount_advanced_rules_id = 1
and #is_rule_percentage != '0') THEN 1 WHEN (dca.discount_advanced_rules_id = 2 and
#is_rule_quota_daily_max != '0') THEN 1 WHEN
(dca.discount_advanced_rules_id = 3 and #is_rule_discount_daily != '0')
THEN 1 WHEN (dca.discount_advanced_rules_id = 4 and
#is_rule_discount_weekly != '0') THEN 1
WHEN (dca.discount_advanced_rules_id = 5
AND #is_rule_discount_monthly != '0') THEN 1 WHEN (dca.discount_advanced_rules_id = 6
AND #is_rule_spesific_payment_method != '0')
THEN 1 WHEN (dca.discount_advanced_rules_id = 7 AND #is_rule_spesific_cc_operator != '0') THEN 1 ELSE 0 END as bb,
NOW()
FROM discount_campaign_advanced dca
WHERE dca.discount_campaign_id = DiscountCampaignId
on duplicate key update id = values(id), discount_campaign_id = values(discount_campaign_id),
discount_advanced_rules_id = values(discount_advanced_rules_id), value = values(value),
createdAt = values(createdAt), status = values(status), updatedAt = values(updatedAt);
END IF;
IF (allow_other_product != 0) THEN
INSERT INTO discount_campaign_advanced (id,discount_campaign_id,
discount_advanced_rules_id, value, createdAt, status, updatedAt)
select *, CASE WHEN
unitData.id is NULL THEN NULL ELSE NOW() END as updatedAt from (SELECT dca.id , DiscountCampaignId, dar.id as dar_id1, CASE WHEN dar.id = 8
THEN #is_allow_other_product END, coalesce(dca.createdAt, NOW()), CASE when (dar.id = 8
and #is_allow_other_product != '0') THEN 1 ELSE 0 END as bb
FROM
discount_advanced_rules dar
LEFT JOIN
discount_campaign_advanced dca ON dca.discount_advanced_rules_id = dar.id
and dca.discount_campaign_id = DiscountCampaignId) as unitData
WHERE unitData.bb = 1
UNION
SELECT dca.id, DiscountCampaignId, dca.discount_advanced_rules_id, CASE WHEN dca.discount_advanced_rules_id = 8 THEN #is_allow_other_product END, coalesce(dca.createdAt, NOW()), CASE when (dca.discount_advanced_rules_id = 8
and #is_allow_other_product != '0') THEN 1 ELSE 0 END as bb,
NOW()
FROM discount_campaign_advanced dca
WHERE dca.discount_campaign_id = DiscountCampaignId
on duplicate key update id = values(id), discount_campaign_id = values(discount_campaign_id),
discount_advanced_rules_id = values(discount_advanced_rules_id), value = values(value),
createdAt = values(createdAt), status = values(status), updatedAt = values(updatedAt);
END IF;
END
but idk why when i call this SP call brambang_uom.update_discount_campaign_advanced2(2757, '5', '5', '1', '1', '1', '1', '1', '9');
it returns Error Code: 1048. Column 'value' cannot be null`, where my wrong at?
Related
MySql select statement with conditional where clause
I am trying to write a MySql statement with a conditional where clause. something like this: set #price = 5000 ; set #city = 1324368075; select count(*) from property where case when #price is not null then price < #price end and (case when #city is not null then CityId = #city end) the variable should be included in the query only if it is not null. My attempts have failed so far. Any ideas? Edited: Sorry I spoke too soon ysth, these two queries are supposed to yield the same count but they dont. Edit #2: Execution plan & indexes Here's the query: set #CountryId = null ; set #CityId = 1324368075 ; set #StateProvince = null ; set #CategoryId = null ; set #TransactionTypeId = null; set #Price = 5000; SELECT Count(*) FROM meerkat.property WHERE (CASE WHEN #CountryId IS NOT NULL THEN CountryId = #CountryId ELSE 1 END) AND (CASE WHEN #CityId IS NOT NULL THEN CityId = #CityId ELSE 1 END) AND (CASE WHEN #CategoryId IS NOT NULL THEN CategoryId = #CategoryId ELSE 1 END) AND (CASE WHEN #StateProvince IS NOT NULL THEN StateProvince = #StateProvince ELSE 1 END) AND (CASE WHEN #TransactionTypeId IS NOT NULL THEN TransactionTypeId = #TransactionTypeId ELSE 1 END) AND (CASE WHEN #Price IS NOT NULL THEN Price <= #Price ELSE 1 END) AND IsPublic = 1 AND IsBlocked = 0; Thanks in advance
If no when conditions are met, case returns null. If you want each test to pass, you need to return a true value instead, so: case when #price is not null then price < #price else 1 end and ...
SQL: return the select value in the stored procedure
I have stored procedure with 3 statements to update sta_fk_col_id in the status table. For test porpuses, I want to return the value in sta_fk_col_id without updating the table. Instead of UPDATE status SET sta_fk_col_id = I tried to do something like SET valido = but without seccuss. SET valido CREATE DEFINER=`tpcroot`#`%` PROCEDURE `sp_test_store_procedure`(IN functionId INT, out valido int) BEGIN -- statement 1.1 SET valido = (SELECT CASE WHEN MAX(mon_alertlevel) >= 60 THEN 3 WHEN MAX(mon_alertlevel) < 60 AND MAX(mon_alertlevel) >= 30 THEN 2 ELSE 1 END AS color FROM monitor INNER JOIN monitor_system ON fk_mon_id = mon_funct_id WHERE fk_dri_id IN (110)) WHERE sta_fk_dri_id = (110); -- statement 1.2 SET valido = (SELECT color FROM ( SELECT MAX(sta_fk_col_id) AS color FROM status WHERE sta_fk_mty_id = 1 AND sta_fk_sys_id = 4 AND sta_fk_dri_id IS NOT NULL) helptable) WHERE sta_fk_sys_id = 4 AND sta_fk_mty_id = 1 AND sta_fk_dri_id IS NULL; -- statement 2 SET valido = (SELECT CASE WHEN MAX(mon_alertlevel) >= 60 THEN 3 WHEN MAX(mon_alertlevel) <60 AND MAX(mon_alertlevel) >= 30 THEN 2 ELSE 1 END AS color FROM monitor_system INNER JOIN monitor ON fk_mon_id = mon_funct_id AND mon_funct_id = functionId) WHERE sta_fk_mty_id = 1 AND sta_fk_sys_id = 4 AND sta_fk_dri_id IS NULL AND (SELECT countDrilldown FROM (select Count(*) AS countDrilldown FROM status WHERE sta_fk_mty_id = 1 AND sta_fk_sys_id = 4 AND sta_fk_dri_id IS NOT NULL) helptable) = 0; select #valido; END I am calling the stored procedure like this: CALL sp_test_store_procedure(2315, #returnValue); select #returnValue;
How to use IF condition in storedProcedure?
I am using a condition to lock the login account after a fixed number of attempts with the wrong password. The update portion is as follows : loginAttempts (INT(1)) is read from login account first DECLARE LoginAttempts INT(1); UPDATE login SET LOGIN_ACCOUNT_STATUS = (SELECT CASE (LoginAttempts > MaxLoginAttempts) WHEN 1 THEN 'LOCKED' ELSE 'ACTIVE' END), LOGIN_LOGIN_ATTEMPTS = (SELECT CASE (#USER_FOUND AND #PASSWORD_CORRECT) WHEN 1 THEN 0 ELSE LOGIN_LOGIN_ATTEMPTS + 1 END), LOGIN_LAST_LOGIN_DATE = (SELECT CASE (#USER_FOUND AND #PASSWORD_CORRECT) WHEN 1 THEN TransactionDateTime ELSE LOGIN_LAST_LOGIN_DATE END), LOGIN_LAST_LOGIN_LOCATION = null WHERE LOGIN_EMAIL = UserEmail; When I set MaxLoginAttmpts at 5, the account gets locked at 11 (Greater than twice maxLoginAttempts). If I set MaxLoginAttmpts at 2, the account gets locked at 5 (Greater than twice maxLoginAttempts). Why is this ? Any help is appreciated. Here I am adding the full stored procedure. CREATE DEFINER=`pubuducg`#`%` PROCEDURE `CustomerAuthenticate`(IN UserEmail VARCHAR(100), IN PassWD VARCHAR(40), IN AccStatus VARCHAR(100),IN TransactionDateTime DATETIME, IN MaxLoginAttempts INT(1)) BEGIN DECLARE LoginUserID INT(11); DECLARE LoginEmail VARCHAR(50); DECLARE LoginPassword TINYTEXT; DECLARE LoginAttempts INT(1); DECLARE AccountStatus VARCHAR(45); DECLARE UserRoles VARCHAR(80); SELECT login.LOGIN_USER_ID, login.LOGIN_EMAIL, login.LOGIN_PASSWORD, login.LOGIN_ACCOUNT_STATUS, login.LOGIN_LOGIN_ATTEMPTS, GROUP_CONCAT(user_role.USER_ROLE_ROLE SEPARATOR ',') AS ROLES INTO LoginUserID, LoginEmail, LoginPassword, AccountStatus, LoginAttempts, UserRoles FROM login INNER JOIN user_role ON user_role.USER_ROLE_USER_ID = login.LOGIN_USER_ID AND user_role.USER_ROLE_STATUS = AccStatus WHERE login.LOGIN_EMAIL = UserEmail; SET #USER_FOUND = found_rows(); SET #PASSWORD_CORRECT = IF((LoginPassword = PassWD AND AccountStatus = AccStatus), true, false); UPDATE login SET LOGIN_ACCOUNT_STATUS = (SELECT CASE (LoginAttempts > MaxLoginAttempts) WHEN 1 THEN 'LOCKED' ELSE 'ACTIVE' END), LOGIN_LOGIN_ATTEMPTS = (SELECT CASE (#USER_FOUND AND #PASSWORD_CORRECT) WHEN 1 THEN 0 ELSE LOGIN_LOGIN_ATTEMPTS + 1 END), LOGIN_LAST_LOGIN_DATE = (SELECT CASE (#USER_FOUND AND #PASSWORD_CORRECT) WHEN 1 THEN TransactionDateTime ELSE LOGIN_LAST_LOGIN_DATE END), LOGIN_LAST_LOGIN_LOCATION = null WHERE LOGIN_EMAIL = UserEmail; SELECT IF(#USER_FOUND AND #PASSWORD_CORRECT, LoginUserID,0) AS USER_ID, #PASSWORD_CORRECT AS AUTHENTICATED, #USER_FOUND AS USER_EXISTS, AccountStatus AS ACCOUNT_STATUS, IF(#USER_FOUND AND #PASSWORD_CORRECT, 0, LoginAttempts + 1) AS LOGIN_ATTEMPTS, IF(#USER_FOUND AND #PASSWORD_CORRECT, UserRoles,null) AS USER_ROLES; END
Linq to SQL generates much slower query
I've got a SQL query that when I run it in SQL Server Mgmt Studio takes 15 seconds to complete. When I translate it to linq and run it in LINQPad it takes 1:50 to complete. Have I just completely bungled the "translation" to linq? This is the SQL: WITH details AS ( SELECT DISTINCT SerialNumber, OrgLevel3, OrgLevel4, [Status Label] FROM vw_PSM_AssetIntransit WHERE yyyyww = DATENAME(yy, GETDATE()) + RIGHT('00' + DATENAME(wk, GETDATE()), 2) AND pro_status <> 'retired' ) SELECT OrgLevel3, OrgLevel4, COUNT(DISTINCT SerialNumber) Total, SUM(CASE WHEN [Status Label] NOT IN ('15-21 Days InTransit', '8-14 Days InTransit', 'over 21 Days InTransit') THEN 1 ELSE 0 END) Compliant FROM details GROUP BY OrgLevel3, OrgLevel4 ORDER BY 1, 2 and this is the LINQ I translated it to: var now = DateTime.Now; var dtf = System.Globalization.DateTimeFormatInfo.CurrentInfo; var calendar = dtf.Calendar; var yearWW = now.Year * 100 + calendar.GetWeekOfYear(now, dtf.CalendarWeekRule, dtf.FirstDayOfWeek); var badStatusLabels = new string[] { "15-21 Days InTransit", "8-14 Days InTransit", "over 21 Days InTransit" }; var details = ( from r in Vw_PSM_AssetIntransit where r.yyyyww == yearWW && r.Pro_status != "retired" select new { r.SerialNumber, r.OrgLevel3, r.OrgLevel4, r.StatusLabel } ).Distinct(); var data = from r in details group r by new { r.OrgLevel3, r.OrgLevel4 } into grp orderby grp.Key.OrgLevel3, grp.Key.OrgLevel4 select new { OrgLevel3 = grp.Key.OrgLevel3, OrgLevel4 = grp.Key.OrgLevel4, Total = grp.Select(x => x.SerialNumber).Distinct().Count(), Compliant = grp.Sum(x => !badStatusLabels.Contains(x.StatusLabel) ? 1 : 0) }; Console.WriteLine(data); This is what LINQPad shows the SQL query came out as: -- Region Parameters DECLARE #p0 Int = 201816 DECLARE #p1 NVarChar(1000) = 'retired' DECLARE #p2 NVarChar(1000) = '15-21 Days InTransit' DECLARE #p3 NVarChar(1000) = '8-14 Days InTransit' DECLARE #p4 NVarChar(1000) = 'over 21 Days InTransit' DECLARE #p5 Int = 1 DECLARE #p6 Int = 0 -- EndRegion SELECT [t3].[OrgLevel3], [t3].[OrgLevel4], ( SELECT COUNT(*) FROM ( SELECT DISTINCT [t5].[SerialNumber] FROM ( SELECT DISTINCT [t4].[SerialNumber], [t4].[OrgLevel3], [t4].[OrgLevel4], [t4].[Status Label] FROM [vw_PSM_AssetIntransit] AS [t4] WHERE ([t4].[yyyyww] = #p0) AND ([t4].[pro_status] <> #p1) ) AS [t5] WHERE ((([t3].[OrgLevel3] IS NULL) AND ([t5].[OrgLevel3] IS NULL)) OR (([t3].[OrgLevel3] IS NOT NULL) AND ([t5].[OrgLevel3] IS NOT NULL) AND ((([t3].[OrgLevel3] IS NULL) AND ([t5].[OrgLevel3] IS NULL)) OR (([t3].[OrgLevel3] IS NOT NULL) AND ([t5].[OrgLevel3] IS NOT NULL) AND ([t3].[OrgLevel3] = [t5].[OrgLevel3]))))) AND ((([t3].[OrgLevel4] IS NULL) AND ([t5].[OrgLevel4] IS NULL)) OR (([t3].[OrgLevel4] IS NOT NULL) AND ([t5].[OrgLevel4] IS NOT NULL) AND ((([t3].[OrgLevel4] IS NULL) AND ([t5].[OrgLevel4] IS NULL)) OR (([t3].[OrgLevel4] IS NOT NULL) AND ([t5].[OrgLevel4] IS NOT NULL) AND ([t3].[OrgLevel4] = [t5].[OrgLevel4]))))) ) AS [t6] ) AS [Total], [t3].[value] AS [Compliant] FROM ( SELECT SUM([t2].[value]) AS [value], [t2].[OrgLevel3], [t2].[OrgLevel4] FROM ( SELECT (CASE WHEN NOT ([t1].[Status Label] IN (#p2, #p3, #p4)) THEN #p5 ELSE #p6 END) AS [value], [t1].[OrgLevel3], [t1].[OrgLevel4] FROM ( SELECT DISTINCT [t0].[SerialNumber], [t0].[OrgLevel3], [t0].[OrgLevel4], [t0].[Status Label] FROM [vw_PSM_AssetIntransit] AS [t0] WHERE ([t0].[yyyyww] = #p0) AND ([t0].[pro_status] <> #p1) ) AS [t1] ) AS [t2] GROUP BY [t2].[OrgLevel3], [t2].[OrgLevel4] ) AS [t3] ORDER BY [t3].[OrgLevel3], [t3].[OrgLevel4]
Stored Procedure is taking more time to execute
I am executing a stored procedure im MYSQL. That will generate 6000 records and insert that 6000 records into a Table. For this, that stored procedure is taking more than 45 min.The file size of stored procedure is 45kb. Systems RAM is 0.98GB. Does system RAM affecting the stored procedure performance? CREATE DEFINER=`root`#`localhost` PROCEDURE `sample`() BEGIN Declare FY_LHS Varchar(20); Declare FY_RHS Varchar(20); Declare abc int default 0; Declare bcd varchar(40) default null; Declare cde int default 0; Declare def varchar(40) default null; Declare efg int default 0; Declare ghi varchar(40) default null; Declare ijk varchar(40) default null; Declare hij varchar(40) default null; Declare ijk varchar(40) default null; Declare param_month int ; Declare monthsortsequence int default 0; Declare p int default 0; Declare q int default 0; Declare r int default 0; Declare s int default 0; Declare t int default 0; DECLARE done INT DEFAULT FALSE; declare dateofdisch int default 0; Declare fId int default 0; DECLARE fac_cur CURSOR FOR SELECT r FROM u ; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; SET param_month = Month(CURDATE()); IF param_month = 4 THEN SET monthsortsequence = 1; ELSEIF param_month = 5 THEN SET monthsortsequence = 2; ELSEIF param_month = 6 THEN SET monthsortsequence = 3; ELSEIF param_month = 7 THEN SET monthsortsequence = 4; ELSEIF param_month = 8 THEN SET monthsortsequence = 5; ELSEIF param_month = 9 THEN SET monthsortsequence = 6; ELSEIF param_month = 10 THEN SET monthsortsequence = 7; ELSEIF param_month = 11 THEN SET monthsortsequence = 8; ELSEIF param_month = 12 THEN SET monthsortsequence = 9; ELSEIF param_month = 1 THEN SET monthsortsequence = 10; ELSEIF param_month = 2 THEN SET monthsortsequence = 11; ELSEIF param_month = 3 THEN SET monthsortsequence = 12; END IF; OPEN fac_cur; read_loop: LOOP FETCH fac_cur INTO fId; IF fId >= 65 THEN LEAVE read_loop; END IF; IF fId != 0 THEN IF MONTH(CURDATE())>=4 THEN Set FY_LHS = YEAR(CURDATE()); Set FY_RHS = YEAR(CURDATE())+1; ELSE Set FY_LHS = YEAR(CURDATE())-1; Set FY_RHS = YEAR(CURDATE()); END IF; Select tblbc.StateId, tblst.StateName, tblbc.DistrictId, tblst.District, tblbc.kID, tblst.k, tblbc.FacilityName, tblbc.FacilityType, tblbc.ReportingToId into abc, bcd, cde, def, efg, ghi, ijk, hij, ijk from u tblbc inner join tblstateinfo tblst on tblbc.stateid = tblst.stateid and tblbc.DistrictId = tblst.dcode and tblbc.kID = tblst.tcode where tblbc.r = fId; Delete from data where Year = concat(FY_LHS,'-',FY_RHS) and Month = param_month and r = fId; Set p = (select count(*) as Count from w tblreg inner join tblp1s tblu on tblreg.p1id = tblu.p1id where tblreg.pm = 0 and (Str_To_Date(tblreg.ty, '%d/%m/%Y') between Str_To_Date(concat(FY_LHS,'/04/01'), '%Y/%m/%d') and Str_To_Date(concat(FY_RHS,'/03/31'), '%Y/%m/%d')) and month(Str_To_Date(tblreg.ty, '%d/%m/%Y')) = param_month and tblu.r = fId); Set q = (Select count(ytid) as Count From w reg inner join tblp1s tblu on reg.p1id = tblu.p1Id where datediff(CurDate(), Str_To_Date(reg.LMP, '%d/%m/%Y')) < 300 and reg.pm = 0 and (Str_To_Date(reg.ty, '%d/%m/%Y') between Str_To_Date(concat(FY_LHS,'/04/01'), '%Y/%m/%d') and Str_To_Date(concat(FY_RHS,'/03/31'), '%Y/%m/%d')) and month(Str_To_Date(reg.ty, '%d/%m/%Y')) = param_month and tblu.r = fId); Set r = (Select count(reg.ytid) from (Select ts.p1id, ts.ytid, count(ts.ytid) as count from tblhjlist ts where ts.fgtype = 'ANC' and ts.actualdateofaction is not null and (Str_To_Date(actualdateofaction, '%d/%m/%Y') between Str_To_Date(concat(FY_LHS,'/04/01'), '%Y/%m/%d') and Str_To_Date(concat(FY_RHS,'/03/31'), '%Y/%m/%d')) and month(Str_To_Date(actualdateofaction, '%d/%m/%Y')) = param_month group by ts.ytid, ts.p1id) tss left join w reg on tss.ytid = reg.ytid inner join tblp1s tblu on reg.p1Id = tblu.p1id where tss.count = 3 and tblu.r = fId); Set s = (Select count(sl.ytid) from (Select p1id, ytid from tblhjlist where fgid = 1 and fgtype = 'FT' and actualdateofaction is not null and (Str_To_Date(actualdateofaction, '%d/%m/%Y') between Str_To_Date(concat(FY_LHS,'/04/01'), '%Y/%m/%d') and Str_To_Date(concat(FY_RHS,'/03/31'), '%Y/%m/%d')) and month(Str_To_Date(actualdateofaction, '%d/%m/%Y')) = param_month) sl inner join w reg on sl.ytid = reg.ytid inner join tblp1s tblu on reg.p1id = tblu.p1id and reg.pm = 0 and tblu.r = fId); Set t = (Select count(ts.ytid) from (Select p1id, ytid from tblhjlist where ((fgtype = 'FT' and fgid = 2) or (fgtype = 'FTBooster' and fgid = 3)) and actualdateofaction is not null and (Str_To_Date(actualdateofaction, '%d/%m/%Y') between Str_To_Date(concat(FY_LHS,'/04/01'), '%Y/%m/%d') and Str_To_Date(concat(FY_RHS,'/03/31'), '%Y/%m/%d')) and month(Str_To_Date(actualdateofaction, '%d/%m/%Y')) = 9 group by ytid) ts inner join w reg on ts.ytid = reg.ytid inner join tblp1s tblu on reg.p1Id = tblu.p1Id and reg.pm = 0 and tblu.r = fId); . . . . . 40 queries insert into data (year, month, stateId, state, districtId, district, kid, k, facilityname, facilityType, sortsequenceMonth, r, sortsequence, itemRefno, itemvalue, ReportingToId) values (concat(FY_LHS,'-',FY_RHS), param_month, abc, bcd, cde, def, efg, ghi, ijk, hij, monthsortsequence, fId, 2, '1.1.1', q, ijk); insert into data (year, month, stateId, state, districtId, district, kid, k, facilityname, facilityType, sortsequenceMonth, r, sortsequence, itemRefno, itemvalue, ReportingToId) values (concat(FY_LHS,'-',FY_RHS), param_month, abc, bcd, cde, def, efg, ghi, ijk, hij, monthsortsequence, fId, 1, '1.1', p, ijk); insert into data (year, month, stateId, state, districtId, district, kid, k, facilityname, facilityType, sortsequenceMonth, r, sortsequence, itemRefno, itemvalue, ReportingToId) values (concat(FY_LHS,'-',FY_RHS), param_month, abc, bcd, cde, def, efg, ghi, ijk, hij, monthsortsequence, fId, 3, '1.3', r, ijk); insert into data (year, month, stateId, state, districtId, district, kid, k, facilityname, facilityType, sortsequenceMonth, r, sortsequence, itemRefno, itemvalue, ReportingToId) values (concat(FY_LHS,'-',FY_RHS), param_month, abc, bcd, cde, def, efg, ghi, ijk, hij, monthsortsequence, fId, 4, '1.4.1', s, ijk); insert into data (year, month, stateId, state, districtId, district, kid, k, facilityname, facilityType, sortsequenceMonth, r, sortsequence, itemRefno, itemvalue, ReportingToId) values (concat(FY_LHS,'-',FY_RHS), param_month, abc, bcd, cde, def, efg, ghi, ijk, hij, monthsortsequence, fId, 5, '1.4.2', t, ijk); . . . . 40 insert statements set fId = fId + 1; END IF; END LOOP; CLOSE fac_cur; END
You need to check which statement exactly in the stored procedure is taking time by adding some debug points (ex: You can insert timestamp after each select into some temp table). You need to check that you have proper indexes on table for select queries.