Create an Event inside a procedure - mysql

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

Related

Trigger that creates a scheduled event when is triggered - Mysql

I have a trigger that updates another table, when it is fired, also, I want to create an scheduled event which is set at the TIME that is SELECTED from the table Assignment using the foreign key relation.
Tables looks like that:
ClassAssignment (Bridge table between Class and Assignment):
classID,
assignmentID
Assignment:
id
deadline DATETIME
CREATE TRIGGER newClassAssignment AFTER INSERT ON classassignment FOR EACH ROW
Begin
INSERT INTO StudentAssignmentSolution(studentID, assignmentID)
SELECT s.ID, NEW.assignmentID
FROM Student s
WHERE s.classID = NEW.classID;
CREATE EVENT deadlineMissed
ON SCHEDULE (select deadline from assignment where id = new.assignmentID)
DO
update StudentAssignmentSolution set solutionDate = CURDATE() assignmentId = new.assignmentID;
END
Hope it makes sense. Thank you =)
No, it's impossible in this form. The event 'schedule' parameter is literal, you can't use a subquery in it. And you can't use the output generated by this subquery - the trigger can't contain dynamic SQL.
The solution:
You create some service table.
When your trigger is fired, it inserts the data necessary to create the event into this table.
You create a basic event procedure that is scheduled often enough. This procedure takes data from the service table and, using dynamic SQL, creates the necessary event procedure.

Call a stored procedure every 5 seconds in MySQL

I have written a stored procedure in MySQL and I want to execute it every 5 seconds, but when I simply run the stored procedure, it doesn't add any records to another table. But if I run the query separately, it works fine but in stored procedure it doesn't.
And how can I call it every 5 seconds.
Here is the stored procedure
DELIMITER //
CREATE PROCEDURE ctrdata2.call_detail()
begin
INSERT INTO ctrdata2.reporting_call_detail
SELECT *
FROM ctrdata2.call_detail
WHERE EXISTS
(
SELECT end_time
FROM ctrdata2.transactions_reporting_call_detail
WHERE call_detail.initiationtimestamp > transactions_reporting_call_detail.end_time);
END
Try adding the below code too.
CREATE EVENT runEveryFiveSeconds
ON SCHEDULE EVERY 5 SECOND
DO
CALL ctrdata2.call_detail();

Display result of MYSQL event scheduler in an interface

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.

Mysql event schedule run evry month

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 ! )

Trigger Error , MySQL

Well, I have researched and have not found a solution to the following problem 'SQL Error (1442): Can not update table' messages' in stored function / trigger because it is already used by statement Which invoked this stored function / trigger. `
My trigger is created, only when I run the INSERT on the table of messages, this error is thrown, my trigger is
DELIMITER $$
DROP TRIGGER IF EXISTS `onMessage` ;
CREATE TRIGGER `onMessage` BEFORE INSERT ON `messages` FOR EACH ROW
BEGIN
UPDATE `users` SET `users`.`messages` = ( `users`.`messages` + 1 )
WHERE `users`.`uid` = NEW.uid ;
DELETE FROM `messages` WHERE `date` < ( NOW( ) - INTERVAL 1 MINUTE ) ;
END ;
$$
DELIMITER ;
This is a restriction in the use of triggers.
From the MySql documentations:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
Correct, for MySQL anyway
You can't write to the table associated with a trigger in that trigger. It generates all manner of inconsistencies, not least because MySQL triggers are processed row-by-row (RBAR)
In this case, I'd use a stored procedure to deal with the INSERT on messages
It is not possible to update a table for which the trigger is created in the trigger since Mysql does some locking stuff on that table. Therefore you can't insert/update/delete rows of the same table because then the trigger would called again and again, ending up in a recursion.
If you want to create a trigger on the table which will update itself (perfoming a deletion in this case), make sure you use the NEW.column_name to refer to the row after it’s updated.