No worries man.
BEAR IN MIND that I am pretty novice still to the SPs and such... in fact, that's the 3rd one I wrote. I built phpMyIDE so that I could learn how to do the things. The honest truth is that my understanding is purely theoretical, me having obtained such theories from hanging around the likes of truly smart people like the DBDog.
OK stop laughing ya bastard.
Anyhoo, there's a problem with the way that the SQL language handles errors - it's *kind of like* an exception handler, but not really. What you do is declare a continue handler in the beginning of the block - you can see them here:
declare continue handler for not found set keepGoing = 0;
declare continue handler for 1062 set dummy = 1;
These two are arguable the most popular - 1062 is a Duplicate Key error and "not found" is, well, when a select does not find either one or anymore of <what you are selecting>. Essentially you are saying "execute this single statement when an error of (x) type occurs.
So the way that I use it is to handle my while loop. I set keepGoing to 1, then do a query - then do a while loop while keepGoing is equal to 1... meaning that if I didn't find one on the first query I'll never enter the loop. Then the last statement of the loop is to do the select again, which will either produce a record or set keepGoing to 0, in which case I bomb out. Here again:
set keepGoing = 1;
select id, siteid, duration, pagecount into thisID, thisSiteID, thisDuration, thisPageCount
from sessions where lasttouch < cutoff limit 1;
-- I won't even start here if the last query found nothing
while keepGoing = 1 do
update track_sitestats set
total_visitors=total_visitors+1,
total_pages=total_pages+thisPageCount,
total_duration=total_duration+thisDuration
where site_id=thisSiteID and datestamp=theDate;
delete from sessions where id=thisID;
select id, siteid, duration, pagecount into thisID, thisSiteID, thisDuration, thisPageCount
from sessions where lasttouch < cutoff limit 1;
end while;
Set dummy=0 is my novice way of saying "ignore the error" - dummy has no role at all except to get set when I see a duplicate key. When that happens, I set dummy to 1 which does... wait for it... nothing. Essentially I'm blindly inserting a record where I know that it's either going to work or bomb on a duplicate PKey error - I don't really care, since I just go on in the next statement and get the ID of the record I'm looking for.
Hope that helps... it's not particularly pretty, that much is certain. However, I now have a bunch of stored procedures working in my new framework and I am REALLY REALLY REALLY pleased about the stability and performance of everything. I also have a bunch of views that I used from the command line and I'm starting to seriously exercise the IDE. I think that time learning this stuff and re-thinking the overall architecture of what a lot of us do is a good idea. An *awful* lot of what we do is data-bound or data-only mechanics - it seems only logical to go right to the source and perform logic.
Cheers,
/p