In an inherited database I have two tables of current interest, records
and logins
. login
tells me the location of the user and where and when his login occured (in and out times included).
The fun part is that there is no correlating mark between the login session and the applications ran. To solve the issue I'm creating a new table login_apps
and am trying to use a function to combine the two tables.
So far I have:
drop function if exists ltop;
delimiter $$
create function ltop(id int, u varchar(10), s datetime, e datetime)
returns text
language sql
begin
declare pid varchar(40);
declare pname varchar(63);
declare my_return varchar(100);
declare cur1 cursor for (select usageProgramID
from records
where usageUser=u
and usageWhen>=s
and usageWhen<=e
and usageProgramID!="");
open cur1;
read_loop: loop
fetch cur1 into pid;
set pname = (select programName from Programs where programID=pid);
insert into login_apps(`sid`, `programName`) values(id, pname);
end loop read_loop;
close cur1;
return "finished";
end;$$
delimiter ;
It enters into the system without issue and the insert is working as expected on a test. The issue I'm coming up with is that when it's run as:
select ltop("265548", "user", "2013-02-21 13:54:27", "2013-02-21 14:32:18");
I get the error:
ERROR 1329 (02000): No data - zero rows fetched, selected, or processed
Shouldn't the data be returned be "finished"?
Secondly, I'm not really sure what would be considered the best way to run this on every value of logins
any pointers for going that route?
SELECT usageProgramID,GROUP_CONCAT(programName) FROM (select usageProgramID from records where usageUser='user' and usageWhen>='2013-02-21 13:54:27' and usageWhen<='2013-02-21 14:32:18' and usageProgramID!="") A INNER JOIN Programs B ON A.usageProgramID = B.programID GROUP BY usageProgramID HAVING COUNT(1) > 1;
. What do you get back ???