2011-11-19 81 views
0

我想在MySQL限制中使用變量,但它是無效的。爲什麼? (如果我改變變量數,這是確定。)MySQL限制變量

MySQL Error> #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@records-leaveErr; END IF; END IF; END' at line 9 

我的代碼:

DELIMITER // 
    CREATE PROCEDURE `deleteOldErrRecords`(backTime int(11), leaveErr int(11)) 
    BEGIN 
     SELECT @records:=COUNT(*) FROM `sys_proc_err`; 
     IF @records > leaveErr THEN 
     SELECT @actRecords:=COUNT(*) FROM `sys_proc_err` WHERE `time` > (NOW()-backTime); 
     IF @actRecords > leaveErr THEN 
      DELETE FROM `sys_proc_err` WHERE `time` < (NOW()-backTime); 
     ELSE 
      DELETE FROM `sys_proc_err` ORDER BY `id` ASC LIMIT @records-leaveErr; 
     END IF; 
     END IF; 
    END // 
DELIMITER ; 
+0

如果您沒有MySQL 5.5.6+,可以在http://stackoverflow.com/questions/245180/variable-limit-clause-in-mysql中找到許多解決方法。 – Gruber

回答