maybe i solved, if anyone is interested or want to see if there are some bugs here is the code:
DECLARE extcursor CURSOR FOR
SELECT
ip,
starttime,
endtime
FROM
TLC
ORDER BY
starttime,
endtime
DECLARE intcursor CURSOR FOR
SELECT
ip,
starttime,
endtime...