MySql CREATE EVENT Error - mysql

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END$$ DELIMITER' at line 4
CREATE EVENT event_1 ON SCHEDULE EVERY 1 WEEK STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY DO BEGIN
UPDATE
sonice_User
SET
update_chance = "1"; END

use below code:
Delimiter $$
CREATE EVENT event_1 ON SCHEDULE EVERY 1 WEEK
STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY
DO
BEGIN
UPDATE sonice_User
SET update_chance = "1";
END $$
Delimiter ;

CREATE EVENT event_1 ON SCHEDULE EVERY 1 WEEK
STARTS CURRENT_DATE + INTERVAL 6 - WEEKDAY(CURRENT_DATE) DAY
DO
UPDATE sonice_User
SET update_chance = "1";

Related

Create a trigger that checks dates [problem]

I have a problem .. What is required is the creation of a trigger to check if the date of the destruction(dateEmpr) is less than the date of return(dateRetEff), it is increased 10 days to the date of the return
table structure
CREATE TABLE emprunter(
numLivre VARCHAR(5),
dateEmpr DATE,
numInsc VARCHAR(5),
dateRetEff DATE
);
This is my code .. and it tells me it's wrong
CREATE TRIGGER verifier_date
BEFORE INSERT ON emprunter
FOR EACH ROW
BEGIN
if((SELECT DATEDIFF(NEW.dateRetEff, NEW.dateEmpr) from emprunter ) <0) then
dateEmpr = DATE_ADD(OLD.dateEmpr, INTERVAL 10 DAY);
end if;
END;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= DATE_ADD(OLD.dateEmpr, INTERVAL 10 DAY)' at line 6
CREATE TRIGGER verifier_date
BEFORE INSERT ON emprunter
FOR EACH ROW
BEGIN
if NEW.dateRetEff < NEW.dateEmpr then
SET NEW.dateEmpr = NEW.dateEmpr + INTERVAL 10 DAY;
end if;
END;
fiddle

mySql query error when I try to use and IF statement

I'm not able to find the error in MySQL query.
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 10
When I remove:
IF DAYOFWEEK(curdate()) BETWEEN 2 AND 6 THEN
END IF;
Then the query executes fine.
I've tried using tab/space inside my IF statement
CREATE EVENT UPLOADTASK
ON SCHEDULE EVERY '1' DAY
STARTS '2019-07-09 00:00:00'
DO
IF DAYOFWEEK(curdate()) BETWEEN 2 AND 6 THEN
INSERT INTO TABLE1 VALUES
(1,"TASK1",curdate()-1,NULL),
(6.5,"TASK2",curdate()-1,NULL),
(3.0,"TASK3",curdate()-1,NULL),
(8,"TASK44",curdate()-1,NULL);
END IF;
Include your If inside Begin End block
CREATE EVENT UPLOADTASK
ON SCHEDULE EVERY '1' DAY
STARTS '2019-07-09 00:00:00'
DO
BEGIN
IF DAYOFWEEK(curdate()) BETWEEN 2 AND 6 THEN
INSERT INTO TABLE1 VALUES
(1,"TASK1",curdate()-1,NULL),
(6.5,"TASK2",curdate()-1,NULL),
(3.0,"TASK3",curdate()-1,NULL),
(8,"TASK44",curdate()-1,NULL);
END IF;
END

Error creating stored procedure when creating counter

When I run the following SQL
CREATE
PROCEDURE `Calendar`()
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS `Cal` (
`month` INT(11) ,
`year` INT(11)
);
SET #counter := -1;
WHILE (DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL #counter MONTH))) < DATEDIFF(DATE(NOW()), DATE(DATE_SUB(NOW(),INTERVAL 12 MONTH)))) DO
INSERT INTO Cal SELECT DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL #counter:=#counter + 1 MONTH),'%m'),DATE_FORMAT(DATE_ADD(DATE_SUB(NOW(),INTERVAL 12 MONTH), INTERVAL #counter + 1 MONTH),'%Y');
END WHILE;
END
to create and stored procedure, I get the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7
Any idea to fix the syntax error? Thanks in advance.

mysql trigger : check how much time left until timestamp column value expires

in my table i have a column named timstamp_col , i have trigger BEFORE UPDATE on the table .
basically what i want is (in the trigger) to extend timstamp_col by 1 hour if there is more than 1 hour left till the timstamp_col value expires , or by 30 minute if there is less then 1 hour left til timstamp_col expires
im not sure how can i check how much time left (in minutes preferably) till the timestampl value expires ... i want something like this
pleas ignore syntax errors
DELIMITER $$
CREATE TRIGGER update_timestamp BEFORE UPDATE
ON table
FOR EACH ROW BEGIN
IF OLD.timstamp_col - NOW() > 60 THEN
SET NEW.timestamp_col = NOW() + INTERVAL 1 HOUR
END IF ;
IF OLD.timstamp_col - NOW() < 60 THEN
SET NEW.timestamp_col = NOW() + INTERVAL 30 MINUTE;
END IF ;
END$$
DELIMITER ;
you are looking for UNIX_TIMESTAMP , it calculates time in seconds
IF UNIX_TIMESTAMP(OLD.timstamp_col) - UNIX_TIMESTAMP() < 3600 THEN
SET NEW.timestamp_col = NOW() + INTERVAL 30 MINUTE;
END IF ;

Cannot declare variable in MySQL trigger

I am having difficulty creating the following trigger in MySQL:
CREATE TRIGGER TRG_COMPLETE_REMINDER
AFTER UPDATE ON reminders
FOR EACH ROW
BEGIN
DECLARE
new_date DATE;
IF (NEW.complete = 1 AND recurrence <> 'NONE') THEN
CASE recurrence
WHEN '1 WEEK' THEN
SELECT INTO new_date NEW.date + INTERVAL 7 DAY;
WHEN '1 MONTH' THEN
SELECT INTO new_date NEW.date + INTERVAL 1 MONTH;
WHEN '3 MONTH' THEN
SELECT INTO new_date NEW.date + INTERVAL 3 MONTH;
END CASE;
INSERT INTO reminders (description, date, userID, complete, recurrence)
VALUES (NEW.description, new_date, NEW.userID, 0, NEW.recurrence);
END IF;
END;
The issue seems to be occurring where I attempt to declare new_date. MySQL returns the following error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO new_date NEW.date + INTERVAL 7 DAY' at line 8
I would like this trigger to create a new reminder record when the complete attribute changes to 1. The new record should have a date greater than the original record, depending on the value stored in recurrence.
Two corrections fixed the issue:
Correcting the order of clauses in the SELECT statement (i.e.
moving INTO new_date after SELECT new_date + INTERVAL 7 DAY)
Adding DELIMITER // before creating the trigger
Rather than use a variable you can use a CASE expression right in the INSERT statement:
CREATE TRIGGER TRG_COMPLETE_REMINDER
AFTER UPDATE ON reminders
FOR EACH ROW
BEGIN
IF (NEW.complete = 1 AND NEW.recurrence <> 'NONE') THEN
INSERT INTO reminders
(description,
date,
userID, complete, recurrence)
VALUES
(NEW.description,
CASE NEW.recurrence
WHEN '1 WEEK' THEN
NEW.date + INTERVAL 7 DAY
WHEN '1 MONTH' THEN
NEW.date + INTERVAL 1 MONTH
WHEN '3 MONTH' THEN
NEW.date + INTERVAL 3 MONTH
ELSE
NULL
END,
NEW.userID, 0, NEW.recurrence);
END IF;
END;
Best of luck.