portlight
asked on
Calling one stored procedure from another
I am trying to call one stored procedure withing another - using an if statement.
I am getting an error so I believe that I have something out of sequence
ERROR
Unknown table 'l1' in field list
I am getting an error so I believe that I have something out of sequence
CREATE PROCEDURE reportFreeCoolingTrackerCalls (
IN fromDate varchar (50),
IN toDate varchar (50),
IN timeZone varchar (50))
BEGIN
DECLARE startDate varchar (50);
DECLARE endDate varchar (50);
DECLARE mylogID Int;
SET startDate = FROM_UNIXTIME(fromDate/1000);
SET endDate = FROM_UNIXTIME(toDate/1000);
IF (l1.activityId = t2.activityId)
THEN CALL reportFreeCoolingTrackerError (
fromDate,
toDate,
timeZone );
ELSEIF (l1.activityId != t2.activityId)
THEN CALL reportFreeCoolingTracker (
fromDate,
toDate,
timeZone );
END IF;
SELECT l1.activityId,t2.activityId
FROM logs l
INNER JOIN groups g ON g.groupId = l.groupId
LEFT JOIN groups g1 ON g.parentId = g1.groupId
LEFT JOIN groups g2 ON g1.parentId = g2.groupId
LEFT JOIN groups g3 ON g2.parentId = g3.groupId
INNER JOIN activities a ON l.logId = a.logId
INNER JOIN log1644 l1 ON a.activityId = l1.activityId
INNER JOIN log1644 t2 ON t2.recordId = l1.recordid + 1
INNER JOIN items i ON l.logId = i.logId AND i.name LIKE '%KW%'
INNER JOIN users u ON l1.userId = u.userId AND i.name LIKE '%KW%'
WHERE i.itemID = "31985" AND l1.activityId = 1257
AND l1.started
BETWEEN startDate
AND endDate
ORDER BY l1.recordId,l1.started;
END //
DELIMITER ;
ERROR
Unknown table 'l1' in field list
Those aliases, l1 and l2, exist only in the context of the SELECT statement that creates them.
ASKER
Okay Is there a way to fix this?
This is untested, but should get you a step closer.
CREATE PROCEDURE reportFreeCoolingTrackerCalls (
IN fromDate varchar (50),
IN toDate varchar (50),
IN timeZone varchar (50))
BEGIN
DECLARE startDate varchar (50);
DECLARE endDate varchar (50);
DECLARE mylogID Int;
DECLARE @ActivityID1 int;
DECLARE @ActivityID2 int;
SET startDate = FROM_UNIXTIME(fromDate/1000);
SET endDate = FROM_UNIXTIME(toDate/1000);
SELECT @ActivityID1 = l1.activityId, @ActivityID2 = t2.activityId
FROM logs l
INNER JOIN groups g ON g.groupId = l.groupId
LEFT JOIN groups g1 ON g.parentId = g1.groupId
LEFT JOIN groups g2 ON g1.parentId = g2.groupId
LEFT JOIN groups g3 ON g2.parentId = g3.groupId
INNER JOIN activities a ON l.logId = a.logId
INNER JOIN log1644 l1 ON a.activityId = l1.activityId
INNER JOIN log1644 t2 ON t2.recordId = l1.recordid + 1
INNER JOIN items i ON l.logId = i.logId AND i.name LIKE '%KW%'
INNER JOIN users u ON l1.userId = u.userId AND i.name LIKE '%KW%'
WHERE i.itemID = "31985" AND l1.activityId = 1257
AND l1.started
BETWEEN startDate
AND endDate
ORDER BY l1.recordId,l1.started;
IF (@ActivityID1 = @ActivityID2)
THEN CALL reportFreeCoolingTrackerError (
fromDate,
toDate,
timeZone );
ELSEIF (@ActivityID1 != @ActivityID2)
THEN CALL reportFreeCoolingTracker (
fromDate,
toDate,
timeZone );
END IF;
Select @ActivityID1, @ActivityID2;
END //
DELIMITER ;
Basically, I'm selecting the values you need, sticking them into variables, then calling the stored procedure conditionally, and finally returning those values to the user that you were originally returning.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
When I execute this it actually pulls 3 distinct result sets.
I only need it to pull the result set for the query actually executed
I only need it to pull the result set for the query actually executed