I have written event scheduler in MySQL. After executing, this event is not working at all. The task which is written is not happening. Below is my event scheduler,
DELIMITER $$
CREATE EVENT `Untravelled_Deduction` ON SCHEDULE EVERY 1 DAY STARTS '2016-04-01 06:42:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
DECLARE UserId INT;
DECLARE v_finished INT DEFAULT 0;
DECLARE GetDate DATE DEFAULT DATE(NOW());
/*get each user who's account is activated and not swiped for the given date*/
DECLARE UnTravelled CURSOR FOR
SELECT DISTINCT U.user_id
FROM `um.user` U
INNER JOIN `um.user_ps.pass` UP ON UP.user_id=U.user_id
INNER JOIN `ps.pass` P ON P.pass_id=UP.pass_id AND P.status_id=4
INNER JOIN `um.user_trs.tariff` UT ON UT.user_id = U.user_id
WHERE U.is_verified_email=1 AND U.is_active=1
AND UT.user_tariff_id = (
/*check user available_journeys journeys is available or not*/
SELECT MAX(UT2.user_tariff_id) FROM `um.user_trs.tariff` UT2 WHERE UT2.user_id = UT.user_id
AND UT2.available_journeys>0 AND UT2.current_balance>0 AND UT2.end_date>=GetDate
);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished=1;
/*Match th date with holiday list*/
SET #HolidayCount=(SELECT COUNT(*) FROM `holiday_list` WHERE DATE(holiday_date)=GetDate);
/*Match date for saturday and sunday*/
IF DAYOFWEEK(GetDate)!=1 AND DAYOFWEEK(GetDate)!=7 AND #HolidayCount=0 THEN
OPEN UnTravelled;
get_userid:LOOP
FETCH UnTravelled INTO UserId;
IF v_finished=1 THEN
LEAVE get_userid;
END IF;
/*Find user is registered for two way or one way, if #UserRouteCount=2 i.e. two way, #UserRouteCount=1 i.e. one way, */
SET #UserRouteCount = (SELECT COUNT(*) FROM `um.user_rm.route` WHERE user_id = UserId);
/*Get user swipe count to check how many times he swiped for the day. if only one and he is one way then deduct only once*/
SET #UserSwipeCount = (SELECT COUNT(*) FROM `ts.swipe_information` WHERE user_id = UserId AND DATE(swipe_in_time)=GetDate);
/*if user is two way and swiped only once for the day then deduct only once*/
IF (#UserRouteCount=2 AND #UserSwipeCount=1) THEN
SET #RouteCount=1;
ELSE
SET #RouteCount=#UserRouteCount;
END IF;
SET #i=1;
/*Get ticket details on this date for the user*/
SET #TicketCont= (SELECT COUNT(ticket_id) FROM `ts.ticket` WHERE DATE(`issued_on`)=GetDate AND user_id=UserId);
SET #IsInsert=0;
/*Check if any ticket is issued for the user on this date. if not he not travelled and go ahead to deduct*/
IF (#TicketCont=0) THEN
SET #IsInsert=1;
END IF;
/*check if ticket issued once, if he is two way user then decuct once*/
IF (#TicketCont=1 AND #UserRouteCount=2) THEN
SET #IsInsert=1;
END IF;
WHILE #i <= #RouteCount DO
IF (#IsInsert=1) THEN
/*Generate ticket if not exist for given date*/
/*get user current tariff plan*/
SET #UserTariffId = (SELECT user_tariff_id FROM `um.user_trs.tariff` WHERE user_id =UserId AND expired_on >= GetDate AND available_journeys > 0 ORDER BY user_tariff_id LIMIT 1);
IF(#UserTariffId IS NOT NULL)
THEN
SET #PerJourneyCost = (SELECT per_journey_cost FROM `um.user_trs.tariff` WHERE user_tariff_id=#UserTariffId );
SET #TariffCurrentBalance = (SELECT current_balance FROM `um.user_trs.tariff` WHERE user_tariff_id=#UserTariffId );
INSERT INTO `ts.ticket`(user_id,ticket_type_id,ticket_number,issued_on,
amount_charged,user_tariff_id,opening_balance,is_untravelled) VALUES
(UserId,1,'',UTC_TIMESTAMP(),#PerJourneyCost,#UserTariffId, #TariffCurrentBalance,1);
IF #PerJourneyCost IS NOT NULL THEN
/*Update user current tariff balance and number of journeys*/
UPDATE `um.user_trs.tariff` SET current_balance=(current_balance-#PerJourneyCost),
available_journeys =(available_journeys-1) WHERE user_tariff_id = #UserTariffId;
END IF;
END IF;
END IF;
SET #i=#i+1;
END WHILE;
/*Update user balance details and update Updated date in User table*/
UPDATE `um.user` SET updated_on=UTC_TIMESTAMP() WHERE user_id = UserId;
END LOOP get_userid;
CLOSE UnTravelled;
END IF;
END$$
DELIMITER ;
Couldn't rectify the issue, Please suggest any improvements.
Regards
Sangeetha
Creating event is not enough.
Make sure that scheduler is actually enabled:
MariaDB [(none)]> show global variables like 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
If not, try to activate it at runtime:
SET GLOBAL event_scheduler = ON;
or
SET ##global.event_scheduler = ON;
Or in /etc/my.conf (or wherever your config is located), and restart server:
event_scheduler=ON
Official docs:
Event Scheduler Configuration
Event scheduler options
Related
I have created stored-procedure with OUT parameter to handle automate INSERT operations based on values of other tables. I also made stored-function for the same operation. I am facing a problem with execution time for both is heavily different which makes me worried and I don't know why is that.
Following table values in summary:
2016 rows in emp_personal
I am using MariaDB 10.3.14. I am testing with HeidiSQL 10.1.0.5577.
I also tried calling it from Node.JS using MariaDB Node.js Connector. But in both cases, I had almost the same result.
This is Stored Procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_auto_attendance`(
OUT `result` INT
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE empId,done,isHoliday,isEmpHoliday,isWeekOff,isApprovedLeave,insertedRows INT DEFAULT 0;
DECLARE attendance VARCHAR(20) DEFAULT 'Present';
DECLARE empIds VARCHAR(500) DEFAULT '';
-- declare cursor with all employee Ids who are active
DECLARE empIds_cursor CURSOR FOR
SELECT id from emp_personal where isActive=1;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
-- Check if today is holiday
SELECT COUNT(id) INTO isHoliday
FROM apexa_portal.public_holiday_dates
WHERE dateOfHoliday= CURDATE();
OPEN empIds_cursor;
get_id: LOOP
SET attendance = 'Present';
FETCH empIds_cursor INTO empId;
-- Following check if NOT FOUND handler has called, if yes exit loop else continue. NOTE: If you don't write this, it will be infinite loop.
IF done = 1 THEN
LEAVE get_id;
END IF;
-- Check if public holiday for employee
IF isHoliday > 0 THEN
SELECT COUNT(phtd.isActive) INTO isEmpHoliday FROM public_holiday_template_days as phtd
LEFT JOIN emp_office_details as eod ON eod.publicHolidayTemplate=phtd.templateId
LEFT JOIN public_holiday_dates as phd ON phd.pub_holiday_id=phtd.holidayId
WHERE eod.empID=empId AND phd.dateOfHoliday=CURDATE() AND phtd.isActive=1;
IF isEmpHoliday > 0 THEN
SET attendance = 'Public Holiday';
END IF;
END IF;
-- if not public holiday, Check if week off
IF attendance <> 'Public Holiday' THEN
SELECT COUNT(eowd.id) INTO isWeekOff FROM emp_office_working_days as eowd
WHERE DAY=DAYOFWEEK(CURDATE()) AND eowd.empId=empId AND eowd.isActive=0;
IF isWeekOff > 0 THEN
SET attendance = 'Week Off';
END IF;
END IF;
-- if not week off, Check if approved leave
IF attendance <> 'Week Off' THEN
SELECT COUNT(la.id) INTO isApprovedLeave FROM leave_application as la
WHERE la.fromDate <= CURDATE() AND la.toDate >= CURDATE() AND la.empID=empId AND la.leaveStatus='Approved';
IF isApprovedLeave > 0 THEN
SET attendance = 'Approved Leave';
END IF;
END IF;
-- insert attendance
INSERT INTO attendance_master(empID,dateOfAttendance,attendance) VALUES(empId,CURDATE(),attendance);
SET insertedRows = insertedRows + 1;
END LOOP get_id;
CLOSE empIds_cursor;
SET result = insertedRows;
END
This is Stored Function
CREATE DEFINER=`root`#`localhost` FUNCTION `test_auto_att_func`()
RETURNS int(11)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE empId,done,isHoliday,isEmpHoliday,isWeekOff,isApprovedLeave,insertedRows INT DEFAULT 0;
DECLARE attendance VARCHAR(20) DEFAULT 'Present';
DECLARE empIds VARCHAR(500) DEFAULT '';
-- declare cursor with all employee Ids who are active
DECLARE empIds_cursor CURSOR FOR
SELECT id from emp_personal where isActive=1;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
-- Check if today is holiday
SELECT COUNT(id) INTO isHoliday
FROM apexa_portal.public_holiday_dates
WHERE dateOfHoliday= CURDATE();
OPEN empIds_cursor;
get_id: LOOP
SET attendance = 'Present';
FETCH empIds_cursor INTO empId;
-- Following check if NOT FOUND handler has called, if yes exit loop else continue. NOTE: If you don't write this, it will be infinite loop.
IF done = 1 THEN
LEAVE get_id;
END IF;
-- Check if public holiday for employee
IF isHoliday > 0 THEN
SELECT COUNT(phtd.isActive) INTO isEmpHoliday FROM public_holiday_template_days as phtd
LEFT JOIN emp_office_details as eod ON eod.publicHolidayTemplate=phtd.templateId
LEFT JOIN public_holiday_dates as phd ON phd.pub_holiday_id=phtd.holidayId
WHERE eod.empID=empId AND phd.dateOfHoliday=CURDATE() AND phtd.isActive=1;
IF isEmpHoliday > 0 THEN
SET attendance = 'Public Holiday';
END IF;
END IF;
-- if not public holiday, Check if week off
IF attendance <> 'Public Holiday' THEN
SELECT COUNT(eowd.id) INTO isWeekOff FROM emp_office_working_days as eowd
WHERE DAY=DAYOFWEEK(CURDATE()) AND eowd.empId=empId AND eowd.isActive=0;
IF isWeekOff > 0 THEN
SET attendance = 'Week Off';
END IF;
END IF;
-- if not week off, Check if approved leave
IF attendance <> 'Week Off' THEN
SELECT COUNT(la.id) INTO isApprovedLeave FROM leave_application as la
WHERE la.fromDate <= CURDATE() AND la.toDate >= CURDATE() AND la.empID=empId AND la.leaveStatus='Approved';
IF isApprovedLeave > 0 THEN
SET attendance = 'Approved Leave';
END IF;
END IF;
-- insert attendance
INSERT INTO attendance_master(empID,dateOfAttendance,attendance) VALUES(empId,CURDATE(),attendance);
SET insertedRows = insertedRows + 1;
END LOOP get_id;
CLOSE empIds_cursor;
RETURN insertedRows;
END
Time to execute Stored Function
SELECT `test_auto_att_func`();
/* Affected rows: 0 Found rows: 1 Warnings: 0 Duration for 1 query: 0.328 sec. */
Time to execute Stored Procedure
CALL `sp_auto_attendance`(#res);
SELECT #res;
/* Affected rows: 6,049 Found rows: 1 Warnings: 0 Duration for 2 queries: 00:01:21.6 */
Stored Function Output and Stored Procedure Output
Can anyone please explain why this is happening? And if I am doing anything wrong, please let me know how should I correct it?
Thank You.
There should be no difference on performance on similar code when you run the code as a procedure vs as a function.
Most likely explanation (see Affected rows) is that at the the time of the function run, the cursor did not find any active persons whereas in the procedure run did, hence running the loop queries.
This is really baffling me. I am converting a simple procedure from informix into mysql. What it basically does is tell me what the next event is from an event table and a calendar table. In informix the procedure is simple.
FOREACH
SELECT date,weekno,event
INTO l_date,l_week,l_event
FROM event,calendar
WHERE dayno = dayno
AND date = l_today
AND start >= l_now
UNION
SELECT date,weekno,event
FROM event,calendar
WHERE dayno = dayno
AND date > l_today
UNION
SELECT TODAY,9999,9999
FROM event,calendar
WHERE dayno = dayno
AND event = (SELECT MAX(event) FROM event)
ORDER BY 3
if l_event = 9999 then <error> end if;
EXIT FOREACH
END FOREACH
So basically the query finds the next event and returns it. l_today and l_event are parameters that are passed. So on to the mysql version.
looper: BEGIN
DECLARE curs1 CURSOR FOR
SELECT CONCAT("SELECT date, weekno, event FROM event INNER JOIN calendar ON dayno = dayno",
" WHERE date = '", lv_today ,"' AND start >= '", lv_time ,"'",
" UNION SELECT date, weekno, event FROM event INNER JOIN calendar ON dayno = dayno WHERE date > '", lv_today ,"'",
" UNION SELECT DATE(NOW()) AS date, 9999 AS weekno, 9999 AS event FROM event INNER JOIN calendar ON dayno = dayno",
" WHERE (SELECT MAX(event) FROM event) ORDER BY event ");
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN curs1;
loop1: LOOP
FETCH curs1 INTO ldate, lweek, levent;
SELECT ldate, lweek, levent;
LEAVE looper;
END LOOP loop1;
END;
I haven't checked that the rest of the methodology works because I get this error:
Incorrect number of FETCH variables.
Does this mean that I have declare a different variable for each of the query returns? I am new to mysql. If this is the case what would be the best way to solve this conundrum? I have also changed to column and table names.
Many thanks
looper: BEGIN
DECLARE curs1 CURSOR FOR
SELECT eve_date, dia_weekno, eve_event
FROM game_event
INNER JOIN stan_calendar ON eve_abs_dayno = dia_abs_dayno
WHERE eve_date = lv_today
AND eve_start >= lv_time
UNION
SELECT eve_date, dia_weekno, eve_event
FROM game_event
INNER JOIN stan_calendar ON eve_abs_dayno = dia_abs_dayno
WHERE eve_date > lv_today
UNION SELECT DATE(NOW()) AS eve_date, 9999 AS dia_weekno, 9999 AS eve_event
FROM game_event
INNER JOIN stan_calendar ON eve_abs_dayno = dia_abs_dayno
WHERE (SELECT MAX(eve_event) FROM game_event) ORDER BY eve_event;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN curs1;
curs_loop: LOOP
FETCH curs1 INTO lv_date, lv_week, lv_event;
SELECT lv_date, lv_week, lv_event;
LEAVE looper;
CLOSE curs1;
END LOOP curs_loop;
Thank you for answering my question, I have put it back to how I thought it was... and it now works. Here is the loop in full.
https://dev.mysql.com/doc/refman/5.7/en/fetch.html The number of columns retrieved by the SELECT statement must match the number of output variables specified in the FETCH statement so yes but in fact you are only selecting 1 concatenated string - I think your first problem is with the select syntax which doesn't need the concat, the brackets or the quotes (unless you are trying to create a prepared statement for some reason - and even if you were I doubt if the code would be correct)
Simple Cursor
DROP PROCEDURE IF EXISTS EC;
DELIMITER $$
CREATE PROCEDURE `EC`(
IN `inemp_no` varchar(255)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
LOOPER:begin
DECLARE done INT DEFAULT FALSE;
declare ename varchar(20);
declare esalary int default 0;
declare emp_cursor CURSOR FOR
SELECT last_name,salary FROM employees where emp_no= inemp_no ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open emp_cursor;
read_loop: loop
fetch emp_cursor into ename,esalary;
if done then leave read_loop; end if;
insert into debug_table (msg) values(concat('employee:',ename,' earns:',esalary));
end loop;
close emp_cursor;
end $$
DELIMITER ;
MariaDB [sandbox]> truncate table debug_table;
Query OK, 0 rows affected (0.22 sec)
MariaDB [sandbox]> call ec(2);
Query OK, 0 rows affected (0.03 sec)
MariaDB [sandbox]> select * from debug_table;
+----+--------------------------+------+
| id | msg | MSG2 |
+----+--------------------------+------+
| 1 | employee:BBB earns:39500 | NULL |
+----+--------------------------+------+
1 row in set (0.00 sec)
I am getting an error in the following code below. The error is #1329 - No data - zero rows fetched, selected, or processed. What exactly does this mean and what is it that i am doing incorrectly? Thanks
create or replace procedure grade()
begin
declare no,s1,s2,s3,cnt,i,per int(20);
declare c1 cursor for select roll,sub1,sub2,sub3 from student;
set i=0;
select count(*) into cnt from student;
open c1;
while i<=cnt do
fetch c1 into no,s1,s2,s3;
set per=s1+s2+s3/3;
if per>=90 then
update student set grade='A+' where roll=no;
elseif (per<90 and per>=80)then
update student set grade='A' where roll=no;
elseif (per<80 and per>=70)then
update student set grade='B+' where roll=no;
elseif (per<70 and per>=60)then
update student set grade='B' where roll=no;
elseif (per<60 and per>=50)then
update student set grade='C+' where roll=no;
elseif (per<50 and per>=40)then
update student set grade='C' where roll=no;
else
update student set grade='FAIL' where roll=no;
end if;
set i=i+1;
end while;
close c1;
end$$
This logic:
set i = 0;
select count(*) into cnt from student;
while i <= cnt do
is going through the loop 1 extra time. It is on the last trip through the loop that you are getting the error.
If cnt is 3, for instance, then the loops are:
0
1
2
3
You either want:
set i = 1
or alternatively
while i < cnt
In SQL, I would use the first method, because things usually start counting at 1 rather than 0 in SQL -- but they are equivalent.
I have a mysql table that looks like this
| begintime | endtime | begindate | enddate | state |
+------------+----------+------------+----------+-------+
| TIME | TIME | DATE | DATE | string|
+------------+----------+------------+----------+-------+
I have this code
CREATE EVENT Update
ON SCHEDULE
EVERY 1 SECOND
DO
UPDATE tablename SET tablename.state = CASE
WHEN (SELECT CURRENT_TIME() FROM DUAL)>=tablename.begintime AND (SELECT CURDATE()>= tablename.begindate) THEN "Available"
WHEN (SELECT CURRENT_TIME() FROM DUAL)>tablename.endtime AND (SELECT CURDATE()= tablename.enddate) THEN "Finished"
ELSE "Not Available"
END
I want the server to be always checking if any of those statements is true, and I dont know how to remove the event when:
WHEN (SELECT CURRENT_TIME() FROM DUAL)>tablename.endtime AND (SELECT CURDATE()= tablename.enddate) THEN "Finished
Thanks for your help.
please try this one.
SET GLOBAL event_scheduler = ON;
DROP EVENT IF EXISTS Update1;
CREATE EVENT Update1
ON SCHEDULE
EVERY 1 SECOND
DO
UPDATE tablename SET tablename.state =
CASE
WHEN NOW() BETWEEN CONCAT(tablename.begindate,' ',tablename.begintime) AND CONCAT(tablename.enddate,' ',tablename.endtime) THEN "Available"
WHEN NOW() > CONCAT(tablename.enddate,' ',tablename.endtime) THEN "Finished"
ELSE "Not Available"
END;
To drop the event after setting to "Finished" try using something like this
SET GLOBAL event_scheduler = ON;
DROP EVENT IF EXISTS Update1;
DELIMITER //
CREATE EVENT Update1
ON SCHEDULE
EVERY 1 SECOND
DO BEGIN
SELECT #begindate := begindate,
#begintime:= begintime,
#enddate := enddate,
#endtime := endtime FROM tablename;
IF (NOW() BETWEEN CONCAT(#begindate,' ',#begintime) AND CONCAT(#enddate,' ',#endtime)) THEN
UPDATE tablename SET state = 'Available';
ELSEIF (NOW() < CONCAT(#begindate,' ',#begintime)) THEN
UPDATE tablename SET state = 'Not Available';
ELSEIF (NOW() > CONCAT(#enddate,' ',#endtime)) THEN
UPDATE tablename SET state = 'Finished';
DROP EVENT IF EXISTS Update1;
END IF;
END//
DELIMITER ;
I have a Trigger on UPDATE.
What is the correct procedure for referencing attribute from the table that is not updated by the UPDATE SQL command? Is the attribute still in the UPDATE variable? I would like to get the value of that attribute for the updated row.
You can access a values of a column before update and after update in MySQL by using keywords OLD and NEW.
For example if you want to determine whether a value of a column actually has been changed during updated you can do
IF NOT OLD.column_name <=> NEW.column_name THEN
-- do something here
END IF;
Note: <=> is NULL-safe comparison operator in MySQL
BTW: There is no UPDATED virtual table in MySQL. It's from SQL Server.
Here is a SQLFiddle demo. Note that even though update affected all records in the table, only one message has been logged in log table. It's because value for a row with id 2 in the end stayed the same.
UPDATE: to keep your finished flag in sync you need triggers for all events (insert, update, delete).
DELIMITER //
CREATE TRIGGER tg_ai_event
AFTER INSERT ON event
FOR EACH ROW
BEGIN
UPDATE activity a
SET status = (EXISTS(SELECT *
FROM event
WHERE activity = a.activity_id
AND done = 0))
WHERE activity_id = NEW.activity;
END//
CREATE TRIGGER tg_ad_event
AFTER DELETE ON event
FOR EACH ROW
BEGIN
UPDATE activity a
SET status = (EXISTS(SELECT *
FROM event
WHERE activity = a.activity_id
AND done = 0))
WHERE activity_id = OLD.activity;
END//
CREATE TRIGGER tg_au_event
AFTER UPDATE ON event
FOR EACH ROW
BEGIN
IF NOT OLD.activity <=> NEW.activity THEN
-- if activity id was changed for an event then clculate finished flag
-- for both old and new activity id
UPDATE activity a
SET status = (EXISTS(SELECT *
FROM event
WHERE activity = a.activity_id
AND done = 0))
WHERE activity_id IN(OLD.activity, NEW.activity);
ELSE
-- otherwise calculate finished flag only if done flag is changed
IF NOT OLD.done <=> NEW.done THEN
UPDATE activity a
SET status = (EXISTS(SELECT *
FROM event
WHERE activity = a.activity_id
AND done = 0))
WHERE activity_id = NEW.activity;
END IF;
END IF;
END//
DELIMITER ;
Here is SQLFiddle demo