Some names and stuff changed to protect my work.
DELIMITER //
CREATE PROCEDURE glt(IN howMany INT)
BEGIN
DECLARE f VARCHAR(32);
DECLARE done INT DEFAULT 0;
DECLARE curs CURSOR FOR SELECT DISTINCT id FROM tpd;
DECLARE CONTINUE HAN开发者_JS百科DLER FOR NOT FOUND SET done = 1;
OPEN curs;
DROP TABLE IF EXISTS lt;
CREATE TEMPORARY TABLE lt LIKE tpd;
REPEAT
FETCH curs INTO f;
IF NOT done THEN
INSERT INTO lt SELECT * FROM tpd WHERE id = f ORDER BY TIME DESC LIMIT howMany;
END IF;
UNTIL done END REPEAT;
CLOSE curs;
END
The above code gives the following error on a linux machine, but not a mac machine despite both being case-sensitive filesystems and having the same MySQL version:
ERROR 1064 (42000) at line 172: 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 'howMany;
END IF;
UNTIL done END REPEAT;
CLOSE curs;
END' at line 16
Bye
Try to use Prepare Statement for INSERT query. Look at http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
It seems it will look like:
PREPARE stmt FROM 'INSERT INTO lt SELECT * FROM tpd WHERE id = f ORDER BY TIME DESC LIMIT ?';
EXECUTE stmt USING howMany;
You cannot use a parameter in the limit
clause here.
You can only do that when using PDO. In this context that's a syntax error.
精彩评论