I am trying to create a trigger using the date add function in mysql using phpadmin and getting a syntax error; my goal is to automatically add a date in the estimated finish date column ie 'fin_dt' which is 14 days from the date the order is placed. Ie current date plus 14 days. I have come up with the following mysql state but get an error:
DELIMITER $$
CREATE TRIGGER before_insert_orderin BEFORE INSERT ON order_in
BEGIN
SET NEW.fin_dt = SELECT DATE_ADD(CURDATE(), INTERVAL 14 DAY) ;
END $$
Your trigger syntax is wrong. You are missing for each row and also you do not need select to set the value. It should be as
delimiter //
create trigger before_insert_orderin before insert on order_in
for each row
begin
set new.fin_dt = date_add(curdate(),interval 14 day);
end ; //
delimiter ;
Your Trigger syntax near " SET NEW.fin_dt = SELECT DATE_ADD(CURDATE(), INTERVAL 14 DAY)"
SELECT DATE_ADD is Wrong. It should be as
CREATE TRIGGER `before_insert_orderin`
BEFORE INSERT ON `order_in`
FOR EACH ROW
SET NEW.fin_dt=DATE_ADD(NOW(), INTERVAL 14 DAY)
Related
I want to delete rows from the database that are older than 7 days. I went through some questions here on Stack but can't find one that helped at all.
I already found this way to do it:
DELETE FROM users WHERE updateDt < (NOW() - INTERVAL 7 DAY)
And this, to create a event in the database:
DELIMITER $$
CREATE EVENT `delete7DayOldMessages`
ON SCHEDULE EVERY 1 DAY STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
delete from users
where datediff(now(),updateDt)>6;
END;$$
DELIMITER ;
Is it right to transform these two examples above into this:
DELIMITER $$
CREATE EVENT `delete7DayOldMessages`
ON SCHEDULE EVERY 1 DAY STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DELETE FROM users WHERE updateDt < (NOW() - INTERVAL 7 DAY)
DELIMITER ;
?
I am setting up a trigger which reads a timestamp column and adds 7 days to it and then puts the newly calculated date into another column in the same table. The timestamp column is called date_requested and the new column is called return_date. Here is my code so far.
Thanks in advance
DELIMITER //
CREATE TRIGGER external_requests_date_trig BEFORE INSERT ON external_requests
FOR EACH ROW
BEGIN
SET NEW.return_date = DATE_ADD(NEW.date_requested, INTERVAL 7 DAY);
END//
DELIMITER ;
I've made slight modifications to the SQL code provided in an answer to this question: How to delete a MySQL record after a certain time
However, I get a "You have an error in your SQL syntax" error each time I run the query.
create event delete_session
on schedule at current_timestamp + interval 1 day
on completion preserve
do begin
delete from session where date < DATE_SUB(NOW(), INTERVAL 7 DAYS);
end;
The code should create an event to delete entries from the session table after 7 days, but instead gives me this error. Is there actually a problem with the syntax here?
Your CREATE EVENT command and DELETE command is using the ; as delimiter. So your CREATE EVENT command is ending after the DELETE command (before END). You need to set the DELIMITER at the beginning to use another one on the CREATE EVENT command.
-- set the DELIMITER to "|"
DELIMITER |
CREATE EVENT delete_session
ON schedule AT current_timestamp + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE FROM session WHERE `date` < DATE_SUB(NOW(), INTERVAL 7 DAY);
END |
-- set the DELIMITER back to the default.
DELIMITER ;
... and you need to change one more thing:
remove the trailing S on DAYS.
I'm trying to create this event but I can't find the syntax error.
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[CODE EDIT 1]
CREATE
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END;
[EDIT]
This is the syntax error message:
[Err] 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 '' at line 11
I could make it work:
I have to add the DELIMITER and change the syntaxis from END; to END|
Anyway here's the code:
DELIMITER |
CREATE
EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END|
DELIMITER ;
Now I have to test it but I could create it so I guess It's fine.
[EDIT]
The event works like a charm.
I have the following stored procedure to calculate running averages. However when called, it runs forever.
delimiter //
CREATE PROCEDURE run_avg(date_start DATE,date_end DATE,pol_id TINYINT,sta_id TINYINT)
NOT DETERMINISTIC
CONTAINS SQL
BEGIN
TRUNCATE average_table;
WHILE date_start <= date_end DO
INSERT INTO average_table(value_avg)
SELECT AVG(a.value) as val
FROM (SELECT value FROM obs_all_unik
WHERE start_time >=date_start
AND start_time < date_start + INTERVAL 8 HOUR
AND polutant_id=pol_id
AND station_id=sta_id) AS a;
SET date_start = date_start + INTERVAL 1 HOUR;
END WHILE;
END;//
delimiter ;
Any ideas?
INDEX(station_id, polution_id, start_date)
would help performance.
It seems wrong to insert just the averages into the table without also inserting these values: station_id, polution_id, start_date.
The endless loop was due to a wrong definition of date_start as DATE. Since I tried to increment by hours it was returning one and the same value. I changed it to TIMESTAMP and now the problem is gone.