I have created the following stored procedure but my Mysql throws ERROR 1064. What I need to do is for every user that exists in table logbackup, to find the date of his first action, then the date of his last action and calculate the duration. Even though, I know I could do it easily in a different way, I would like some help with any syntax errors that I may have since I am new in mysql.
EDIT:
USE 'databasename'
DROP procedure IF EXISTS `userEngagementCalc`;
DELIMITER $$
USE `databasename`$$
CREATE DEFINER=`root`@`%localhost` PROCEDURE `userEngagementCalc`()
CREATE PROCEDURE userEngagementCalc()
BEGIN
DECLARE v_username varchar(50);
DECLARE startDate datetime;
DECLARE endDate datetime;
DECLARE duration int;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 cursor for
select distinct username
from logbackup;
DECLARE continue handler for not found set done=true;
OPEN cur1;
igmLoop: LOOP
fetch cur1 into v_username;
if done then leave igmLoop; end if;
set @startDate:=(select STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') from logbackup where username=v_username order by STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') asc LIMIT 1);
set @endDate:=(select STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') from logbackup where username=v_username order by STR_TO_DATE(created_date, '%a %b %d %Y %H:%i:%s') desc LIMIT 1);
set @duration:= TIME_TO_SEC(TIMEDIFF(@startDate,@endDate));
INSERT INTO userengagement (username,startDate,endDate,duration) VALUES (v_username,@startDate,@endDate,@duration);
END LOOP igmLoop;
CLOSE cur1;
END$$
DELIMITER;
I am trying to execute this scipt on Ubuntu mysql terminal. Does it need any special manipulation>? What I receive from mysql except from the ERROR messages is something like the following:
Display all 813 possibilities? (y or n)
? DAY_MINUTE INNODB MULTILINESTRINGFROMWKB SESSION_USER VARIANCE
ABS DAY_SECOND INOUT MULTIPOINT SET VARYING
ACOS DEALLOCATE INSENSITIVE MULTIPOINTFROMTEXT SHA VAR_POP
ACTION DEC
I appreciate your kind help.