I have a database containing the name and the associated birthday. I have set an event scheduler to periodically check the database for bithdays within two days from the current date. This is my code-
DELIMITER $$ CREATE EVENT `Reminder` ON SCHEDULE
EVERY 1 Day
ON COMPLETION PRESERVE
ENABLE
DO BEGIN
select * from reminder where date_format(date_sub(Birthday,interval 2 day),'%m-%d') = Date_Format(curdate(),'%m-%d');
END $$
DELIMITER ;
Apparently the sql query does not display any information. I would like to display the result of the query in an interface, whenever the query returns at least one row.
Related
I have a condition, where I want to create an event inside a procedure for multiple Databases.
I want to first create an event from procedure.
I am getting below error,
MySQL said: #1576 - Recursion of EVENT DDL statements is forbidden when body is present
Below is my sample query, Any help please
CREATE PROCEDURE ActivateHolidays()
BEGIN
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
SELECT * from employee;
END
Hello is possible INSERT row to table with expiration date and when the expiration date is reached so row are automatically deleted? Or i only must create one column with expiration date and when sorting checking this value for ignore?
Your need looks more like an Event
IF you want not want to add expiration date column than
CREATE EVENT delete_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE FROM Your_table WHERE date>= logic for expiration_date ;
END;
otherwise
DELIMITER $$
CREATE EVENT Delete ON SCHEDULE EVERY 1 DAY
DO
BEGIN
DELETE FROM TABLE WHERE `date_expires` < CURRENT_TIMESTAMP();
END$$
DELIMITER
;
NOTE that MySQL Event Scheduler need to be enabled on your server:
SET GLOBAL event_scheduler = ON;
You cannot do this directly in the database. You can do this by scheduling an event or job that does the delete. You can schedule an event for each deletion.
I think a better approach, though, is to use a view:
create view v_t as
select t.*
from t
where expiration_date > now();
This ensures that the row is not available at the instant the expiration date is no longer in the future. You can then remove the rows at your leisure. Deleting rows can put a load on the database, so you might want to wait until the load is lighter.
I have the following script written for event schedule. This is my first time with schedule. I am not getting any errors but it doesn't perform the required operation.
CREATE EVENT checkBlack ON SCHEDULE EVERY 5 SECOND DO
DELETE FROM BlackList WHERE id=13;
It displays this message <n>Query: Create EVENT checkBlack2 on Schedule every 5 second DO Delete from BlackList where id=13
0 row(s) affected
But I have a row with id 13.
Help!!
DELIMITER $$
CREATE /*[DEFINER = { user | CURRENT_USER }]*/ EVENT `checkBlack`
ON SCHEDULE EVERY 5 SECOND
DO
BEGIN
DELETE FROM `BlackList` WHERE id=13;
END$$
DELIMITER ;
I have two tables which I use to store call details in. One table (Call_Detail) stores the header details against each call that gets entered, the second (Call_History) stores every comment against the call. So a single call will only appear ONCE in the Call_Detail table, but may appear multiple times in the Call_History table.
I currently run a Query to return the latest comment against a group of calls. So, I return the header details out of Call_Detail and then cross reference against the Call_History to find the 'newest' comment (thanks to some outside help). However, this Query can be quite time consuming when running against a large number of calls.
Therefore, I'm thinking to optimize my Query, I want to setup a trigger that records these details.
I am wanting to catch any INSERT command into the Call_History table and record the comment and date/time into the Call_Detail table against the relevant call ID.
So far I have the following but it doesn't like my syntax for some reason:
DELIMITER $$
CREATE TRIGGER Last_Call_Update
AFTER INSERT ON call_history
FOR EACH ROW
BEGIN
UPDATE call_detail
SET last_updated = NEW.updated_at, last_commment = NEW.body
WHERE id = NEW.ticket_id
END $$
DELIMITER ;
Add semicolon after UPDATE statement
DELIMITER $$
CREATE TRIGGER Last_Call_Update
AFTER INSERT ON call_history
FOR EACH ROW
BEGIN
UPDATE call_detail
SET last_updated = NEW.updated_at, last_commment = NEW.body
WHERE id = NEW.ticket_id;
END $$
DELIMITER ;
DELIMITER $$
ALTER DEFINER=`root`#`localhost` EVENT `update_start_date` ON SCHEDULE EVERY 1 MONTH
DO
UPDATE categories SET start_date=now() where status = 1 $$
DELIMITER ;
The above event runs only one time at the month beginning but next month begining not run, Le me know how run event every month begining.
I think you should use "cron" for this
I would personally advise that you use cron or some other external schedule management system.
IF you must to mySQL to handle schedules:
DELIMITER $$
ALTER DEFINER=`root`#`localhost` EVENT `update_start_date` ON SCHEDULE EVERY 1 MONTH START NOW()
DO
UPDATE categories SET start_date=now() where status = 1 $$
END$$
DELIMITER ;
You will need to see http://dev.mysql.com/doc/refman/5.1/en/events.html for more details, but here is a simple version.
First you need to enabled the scheduler ->
SET GLOBAL event_scheduler = ON;
and then schedule the actual thing
DELIMITER $$
CREATE EVENT IF NOT EXISTS event_name ON SCHEDULE EVERY MONTH
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
ENABLE
COMMENT 'comment'
DO BEGIN
//SQL statements here.
END $$
DELIMITER ;
Please note, that this requires superuser access to your MySQL database (CRON would require super user access to the server ! )