Cannot declare variable in MySQL trigger - mysql

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.

Related

Stored procedure MySQL 5.5.16

So I have in my database a table called "weeks" where I store every weeks of the years like so:
table weeks(id, year, num_week, date_min, date_max)
So for this week, the line look like this :
Note : My weeks starts on thursday and ends on Wenesday.
Since it is a pain inserting each weeks line by line, I want to create a stored procedure for this, here's what I came up with :
DELIMITER |
CREATE PROCEDURE proc_insert_weeks()
BEGIN
SELECT year, num_week, date_min, date_max INTO #year, #num_week, #date_min, #date_max
FROM weeks
ORDER BY date_min DESC LIMIT 1;
SET #date_min = DATE_ADD(#date_max INTERVAL 1 DAY);
SET #date_max = DATE_ADD(#date_min INTERVAL 6 DAY);
SET #year= YEAR(#date_min);
IF #num_week < 52 THEN SET #num_week = #num_week + 1;
ELSE SET #num_week = 1;
END IF;
INSERT INTO weeks (year, num_week, date_min, date_max)
VALUES (#year, #num_week, #date_min, #date_max);
END |
DELIMITER ;
So the idea was to take the last record of the table and add 1 week to the dates, but I can't even make it paste the creation of the procedure.
I get an error right after the SELECT query, can someone help me figure out what I am doing wrong ?
drop PROCEDURE proc_insert_weeks;
DELIMITER |
CREATE PROCEDURE proc_insert_weeks()
BEGIN
SELECT year, num_week, date_min, date_max INTO #year, #num_week, #date_min, #date_max
FROM weeks
ORDER BY date_min DESC LIMIT 1;
SET #date_min = DATE_ADD(#date_max, INTERVAL 1 DAY);
SET #date_max = DATE_ADD(#date_min, INTERVAL 6 DAY);
SET #year= YEAR(#date_min);
IF #num_week < 52 THEN SET #num_week = #num_week + 1;
ELSE SET #num_week = 1;
END IF;
INSERT INTO weeks (year, num_week, date_min, date_max)
VALUES (#year, #num_week, #date_min, #date_max);
END |
DELIMITER ;
You can try above code.
You made mistake in DATE_ADD function. You missed , in it.

SQL: DATE_ADD(date,INTERVAL expr type) skip weekends

I'm currently using DATE_ADD(date,INTERVAL expr type) to set a due date as a trigger in a mySQL Database.
What I'm wanting to know is if it is possible to skip weekends (Saturday, Sunday) as part of the trigger.
You'd have to create an own function for doing that. You can look how to do that in this answer, for example (just use function instead of procedure). As for how to write such a function, here's a working algorithm. The code is quite straightforward: it loops through days and skips weekends.
CREATE FUNCTION `DAYSADDNOWK`(addDate DATE, numDays INT) RETURNS date
BEGIN
IF (WEEKDAY(addDate)=5) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
IF (WEEKDAY(addDate)=6) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
WHILE numDays>0 DO
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
IF (WEEKDAY(addDate)=5) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
IF (WEEKDAY(addDate)=6) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
SET numDays=numDays-1;
END WHILE;
RETURN addDate;
END
Currently SELECT DAYSADDNOWK(CURDATE(), 5) yields 2016-03-07, which is correct.
Of course you only can use it with days, so no arbitrary interval, but your question mentioned date datatype, and I don't quite see how one could add a month not counting working days.
This function simply creates a list of dates starting at the date given in the arguments, and then figures out which date is x number of days (the interval) out while disregarding days 1 and 7 (which are Sunday and Saturday respectively on SQL Server).
CREATE FUNCTION [dbo].[udf_days_add_no_wknd]
(
#start_date date
, #interval int
)
RETURNS date
AS
BEGIN
declare #answer date
; with dates as
(
select #start_date as date_val
union all
select dateadd(d, 1, date_val) as date_val
from dates
where date_val < dateadd(d, #interval * 10, #start_date)
)
, final as
(
select top 1 lead(ld.date_val, #interval, NULL) over (order by ld.date_val asc) as new_date_val
from dates as ld
where 1=1
and datepart(dw, ld.date_val) not in (1,7) --eliminating weekends
)
select #answer = (select new_date_val from final)
return #answer
END
It is worth nothing that this solution is dependent on having SQL Server 2012 or later, considering the use of the lead() function.

SQL - Date field restriction (<=today)

I have a table with date which is entered when the member joins the club. The column is bog standard with DATE as type. No need to have time.
Basically I am trying to find a way that it can not be in the future?
You can use triggers. Below is a fully-functional tested example:
create table t(signup_date date not null);
delimiter //
create trigger ins_check_date before insert on t
for each row
begin
if (new.signup_date > current_date()) then
signal sqlstate '45000' set message_text = 'Date cannot be in the future';
end if;
end//
create trigger up_check_date before update on t
for each row
begin
if (new.signup_date > current_date()) then
signal sqlstate '45000' set message_text = 'Date cannot be in the future';
end if;
end//
delimiter ;
insert into t values (current_date());
insert into t values (current_date() - interval 1 day);
insert into t values (current_date() - interval 2 day);
insert into t values (current_date() + interval 1 day);
select * from t;
update t set signup_date = signup_date - interval 1 month
where signup_date = current_date() - interval 1 day;
update t set signup_date = signup_date + interval 1 month
where signup_date = current_date();
select * from t;

MySQL - Duplicate PK when trigger is fired, but not when record is directly inserted

I have the following MySQL trigger. It is fired for an update on tbl_users. It will either update tbl_user_stats with some information if there already exists a record for that day, or it will insert a new record in tbl_user_stats if there is not a record for that day. action_date is the PK of tbl_user_stats and it holds the date.
drop trigger if exists trigger_update_user_stats_upgrades_and_downgrades$$
CREATE TRIGGER trigger_update_user_stats_upgrades_and_downgrades AFTER UPDATE ON tbl_users
FOR EACH ROW BEGIN
DECLARE date_now int;
IF NEW.premium <> OLD.premium THEN
SET date_now = (SELECT count(*) from tbl_user_stats WHERE DATE(action_date) = CURDATE());
IF date_now > 0 THEN
IF NEW.premium = 0 THEN
UPDATE tbl_user_stats SET downgrades = downgrades + 1 WHERE DATE(action_date) = CURDATE();
ELSE
UPDATE tbl_user_stats SET upgrades = upgrades + 1 WHERE DATE(action_date) = CURDATE();
END IF;
ELSE
IF NEW.premium = 0 THEN
INSERT INTO tbl_user_stats (action_date, downgrades) values (CURDATE() + INTERVAL 1 DAY, 1);
ELSE
INSERT INTO tbl_user_stats (action_date, upgrades) values (CURDATE() + INTERVAL 1 DAY, 1);
END IF;
END IF;
END IF;
END$$
Towards the bottom, I am inserting CURDATE() + INTERVAL 1 DAY for testing purposes. I am trying to simulate an update on tbl_users being performed on the next day. So if today is 11/22/13 I want to pretend that a record in tbl_users is being updated on 11/23/13. If the trigger works correctly, tbl_user_stats should have a new record inserted with the action_date of 11/23/13.
The problem is that when I try to update tbl_users, I get an error that says "Duplicate primary key 11/23/13". However, there is no primary key in tbl_user_stats set to 11/23/13. When I manually insert a new record into tbl_user_stats using 11/23/13 as the PK, there is no problem. The problem only arises when I try to update tbl_users. Why am I getting the Duplicate primary key error?
I have reproduced this error.
This occurs because you're checking condition of "date_now" is checking for CURDATE()
when you're inserting with CURDATE() + INTERVAL 1 DAY
To simulate the next day, you should change all your date comparisons to CURDATE() + INTERVAL 1 DAY
sqlFiddle code with all CURDATE() changed to CURDATE + INTERVAL 1 DAY gets rid of the error.
To reproduce error, just change
SET date_now = (SELECT count(*) from t2 WHERE DATE(start_date) = CURDATE()+ INTERVAL 1 DAY);
to
SET date_now = (SELECT count(*) from t2 WHERE DATE(start_date) = CURDATE());`

How can I store a MySQL interval type?

This is what I would like to be able to do:
SET #interval_type := MONTH;
SELECT '2012-01-01' + INTERVAL 6 #interval_type;
+------------+
|'2012-06-01'|
+------------+
And of course that doesn't work and there is no "interval" data type in MySQL.
I want to be able to store an interval value and an interval type in a table so that i can have the database quickly do the math naturally without having to write a big switch statement, ala
... ELSE IF (type = 'MONTH') { SELECT #date + INTERVAL #value MONTH; } ...
Is this supported in any way in MySQL or do you have a clever hack for this?
Thanks; you rock.
This solution may come handy to somebody implementing the job queue for cron or something similar.
Let us suppose we have a reference date (DATETIME) and interval of repetition. We would like to store both values in database and get the quick comparison whether it's already time to execute and include job into execution queue or not.
The interval could be non trivial e.g. (1 YEAR 12 DAYS 12 HOUR) and is controlled by wise user (admin) so that user is not going to use values exceeding the range of regular DATETIME data type or otherwise the conversion must be implemented first. (18 MONTH -> 1 YEAR 6 MONTH).
We can use then DATETIME data type for storing both values reference date and interval. We can define stored function using:
DELIMITER $$
CREATE DEFINER=`my_db`#`%` FUNCTION `add_interval`(`source` DATETIME, `interval` DATETIME) RETURNS datetime
BEGIN
DECLARE result DATETIME;
SET result = `source`;
SET result=DATE_ADD(result, INTERVAL EXTRACT(YEAR FROM `interval`) YEAR);
SET result=DATE_ADD(result, INTERVAL EXTRACT(MONTH FROM `interval`) MONTH);
SET result=DATE_ADD(result, INTERVAL EXTRACT(DAY FROM `interval`) DAY);
SET result=DATE_ADD(result, INTERVAL EXTRACT(HOUR FROM `interval`) HOUR);
SET result=DATE_ADD(result, INTERVAL EXTRACT(MINUTE FROM `interval`) MINUTE);
SET result=DATE_ADD(result, INTERVAL EXTRACT(SECOND FROM `interval`) SECOND);
RETURN result;
END
We can then make DATETIME arithmetic using this function e.g.
// test solution
SELECT add_interval('2014-07-24 15:58:00','0001-06-00 00:00:00');
// get job from schedule table
SELECT job FROM schedule WHERE add_interval(last_execution,repetition)<NOW();
// update date of executed job
UPDATE schedule SET last_execution=add_interval(last_execution,repetition);
You can solve this problem using prepared statements, considering there is no language construct available for use. The benefit here being you get the performance and flexibility that you want; this could easily be placed in a stored procedure or function for added value:
SET #date = '2012-01-01';
SET #value = 6;
SET #type = 'MONTH';
SET #q = 'SELECT ? + INTERVAL ? ';
SET #q = CONCAT(#s, #type);
PREPARE st FROM #q;
EXECUTE st USING #date, #value;
Alternatively, depending on your database / software architecture and the type of date/time intervals you are thinking of, you could simply this problem by using a time-scale interval:
SELECT #date + INTERVAL #value SECOND
1 second - 1
1 minute - 60
1 hour - 3600
1 day - 86400 (24 hours)
1 week - 604800 (7 days)
1 month - 2419200 (4 weeks)
Here's the simplistic approach. It works reasonably fast. You can change the order of the switch statements to optimize for speed if you feel that you will be hitting some more often then others. I have not benched this against Chris Hutchinson's solution. I ran into problems trying to wrap it into a nice function because of the dynamic SQL. Anyway, for posterity, this is guaranteed to work:
CREATE FUNCTION AddInterval( date DATETIME, interval_value INT, interval_type TEXT )
RETURNS DATETIME
DETERMINISTIC
BEGIN
DECLARE newdate DATETIME;
SET newdate = date;
IF interval_type = 'YEAR' THEN
SET newdate = date + INTERVAL interval_value YEAR;
ELSEIF interval_type = 'QUARTER' THEN
SET newdate = date + INTERVAL interval_value QUARTER;
ELSEIF interval_type = 'MONTH' THEN
SET newdate = date + INTERVAL interval_value MONTH;
ELSEIF interval_type = 'WEEK' THEN
SET newdate = date + INTERVAL interval_value WEEK;
ELSEIF interval_type = 'DAY' THEN
SET newdate = date + INTERVAL interval_value DAY;
ELSEIF interval_type = 'MINUTE' THEN
SET newdate = date + INTERVAL interval_value MINUTE;
ELSEIF interval_type = 'SECOND' THEN
SET newdate = date + INTERVAL interval_value SECOND;
END IF;
RETURN newdate;
END //
It comes with this equally simplistic benchmark test:
CREATE FUNCTION `TestInterval`( numloops INT )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE date DATETIME;
DECLARE newdate DATETIME;
DECLARE i INT;
SET i = 0;
label1: LOOP
SET date = FROM_UNIXTIME(RAND() * 2147483647);
SET newdate = AddInterval(date,1,'YEAR');
SET i = i+1;
IF i < numloops THEN
ITERATE label1;
ELSE
LEAVE label1;
END IF;
END LOOP label1;
return i;
END //