2013-12-19 71 views
1

我想調用另一個存儲過程 - 使用if語句。 我得到一個錯誤,所以我相信,我有一些失序一個存儲過程調用另一個 - 但是得到一個表錯誤

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

未知表中的字段列表「L1」

回答

0

表的別名(如INNER JOIN log1644 l1 ON...僅在聲明中可見(在你的情況下,SELECT),其中包含他們。

你需要參考完整的表名以外select。

至於邏輯....你需要在SP調用中使用select來填充需要的值,並將它們填充到變量中。或者,在調用中使用select子查詢,但這將是一個維護頭痛!

2

我覺得這個代碼是造成麻煩你:

IF (l1.activityId = t2.activityId) --Here l1 and t2 are not known 
    THEN CALL reportFreeCoolingTrackerError (
    fromDate, 
    toDate, 
    timeZone); 
ELSEIF (l1.activityId != t2.activityId) --Here l1 and t2 are not known 
    THEN CALL reportFreeCoolingTracker (
    fromDate, 
    toDate, 
    timeZone); 
     END IF; 

如同在這一行l1是未知的。同樣T2也不得而知

相關問題