How to schedule MYSQL Event on Specific Datetime? - mysql

I am facing an issue want to updating the message table column DateTime using the MySQL event scheduler On DateTime '2022-02-23 23:58:00' where status is open and mes_type is SSG. Please help me to correct my code I will be thankful to you.
DELIMITER $$
CREATE EVENT my_event
ON SCHEDULE AT '2022-02-24 23:58:00'
DO
UPDATE MESSAGE
SET datetime = '2022-03-01 23:59:00'
WHERE datetime = '2022-02-23 23:59:00'
AND STATUS = 'OPEN' and MES_TYPE = 'SSG'
END */$$
DELIMITER ;

You have only 1 statement so you don't need to set delimiters and the end statement is unnecessary but you do need to terminate the update
CREATE EVENT my_event
ON SCHEDULE AT '2022-02-24 23:58:00'
DO
UPDATE MESSAGE
SET datetime = '2022-03-01 23:59:00'
WHERE datetime = '2022-02-23 23:59:00'
AND STATUS = 'OPEN' and MES_TYPE = 'SSG';

Related

Changing the value of a field from a table when another is changed using triggers

Good morning everyone.
I have a table like this in mysql:
CREATE TABLE NC
(
ID INTEGER AUTO_INCREMENT NOT null,
reportingDate DATE,
closingDate DATE,
State VARCHAR(20),
PRIMARY KEY(CodNC)
)
What I need is to automatically changing the value of the field "ClosingDate" up to today everytime the value of the field "State" is changed with "Closed" using TRIGGERS.
Thanks
This looks like a simple before update trigger:
delimiter //
create trigger trg_nc_before_state_update
before update on nc
for each row
begin
if new.state = 'Closed' and not old.state <=> 'Closed' then
set new.closingdate = current_date;
end if;
end
//
delimiter ;

Mysql Event Scheduler : Table Does not exist error , despite existing

Looking for some advice please :
I have a MySQL 5.6 Event scheduler set up as below:
DELIMITER $$
CREATE DEFINER=`root`#`10.0.0.%` EVENT `update_reports` ON SCHEDULE EVERY 3 MINUTE STARTS '2015-10-16 13:01:20' ON COMPLETION PRESERVE ENABLE DO update_reports:BEGIN
DECLARE triggered datetime DEFAULT SYSDATE();
DECLARE started datetime DEFAULT NULL;
DECLARE status tinyint(1) DEFAULT 0;
# LOGIC
IF wait_for_processing('reports.processing') THEN
SET started = SYSDATE();
# SET SESSION VARIABLES
SELECT enable_logging(false);
SELECT enable_debug_timers(true);
SET #reporting_from = '2013-03-01 00:00:00';
IF (needs_update()) THEN
SELECT update_usage_data();
SET status = 1;
ELSE
SET status = 2;
END IF;
SELECT free_for_processing('reports.processing');
END IF;
INSERT INTO ref.event_logs
SELECT
'reports',
'update_reports',
triggered,
started,
SYSDATE(),
null,
status;
END update_reports
However, in the MySql Error log I am receiving this error:
2015-10-16 13:37:20 29327 [ERROR] Event Scheduler: [root#10.0.0.%][reports.update_reports] Table 'reports.reporting_statsevent_logsuser_seen' doesn't exist
2015-10-16 13:37:20 29327 [Note] Event Scheduler: [root#10.0.0.%].[reports.update_reports] event execution failed.
Permissions on the database and tables show root#10.0.0.% to have full permissions.
Could anyone please share any troubleshooting steps I can take to understand why the event scheduler cannot access the tables, yet the queries work fine when carried out in MySql Workbench.
Thank you.

MYSQL - check a time and execute query

I'd like (if it possible) check a time and if time = special time, make update of the table.
My idea:
select current_time()
if current_time = 15:19
{
update tasks set finished = replace (finished, '1', '0')
}
Do you have any ideas???
Use an event for that
delimiter //
CREATE EVENT IF NOT EXISTS your_event
ON SCHEDULE EVERY 1 DAY
STARTS '2014-07-04 15:19:00'
ON COMPLETION PRESERVE ENABLE
DO
update tasks
set finished = replace (finished, '1', '0');
//
It will be executed automatically every day on the defined time.

Mysql current date trigger

I have the following trigger
CREATE TRIGGER `qc_date_trigger` AFTER UPDATE ON `brand`
FOR EACH ROW BEGIN
IF NEW.brandQC = '1' THEN
SET #brandQCDate = CURDATE();
ELSE
SET #brandQCDate = NULL;
END IF;
END
For some reason it does not update my Date field into the current date when QC is = 1. I've checked the mysql doc and it should work. Any ideeas?
If your column is named brandQCDate, then remove the #. The # makes it a user defined variable, not the column. Also you want to make this a BEFORE UPDATE trigger.
CREATE TRIGGER `qc_date_trigger` BEFORE UPDATE ON `brand`
FOR EACH ROW BEGIN
IF NEW.brandQC = '1' THEN
SET NEW.brandQCDate = CURDATE();
ELSE
SET NEW.brandQCDate = NULL;
END IF;
END

Trigger: Referencing updated attribute

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