我正在使用 PDO 调用以 DROP TABLE IF EXISTS 开头的存储过程。我随机得到 PDOException 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'historygr.reached' doesn't exist' 更烦人的是它会从告诉我到抛出异常说表已经存在,在几秒钟之内,似乎来自同一个连接。
我自己无法触发错误,但我会收到错误通知。
这是错误源自的 PHP:
$dbh = PDODB::getInstance();
$stmt = $dbh->query("CALL ListReached(".$this->item_id.")"); // <-- ERROR
$items = $stmt->fetchAll();
这是 MySQL 过程定义:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ListReached`( IN root INT)
BEGIN
DECLARE rows SMALLINT DEFAULT 0;
DROP TABLE IF EXISTS reached;
CREATE TABLE reached(
node_id INT PRIMARY KEY
) ENGINE=HEAP;
INSERT INTO reached VALUES (root);
SET rows = ROW_COUNT();
WHILE rows > 0 DO
INSERT IGNORE INTO reached
SELECT DISTINCT child_id
FROM related_item AS r
INNER JOIN reached AS p ON r.parent_id = p.node_id;
SET rows = ROW_COUNT();
INSERT IGNORE INTO reached
SELECT DISTINCT parent_id
FROM related_item AS r
INNER JOIN reached AS p ON r.child_id = p.node_id;
SET rows = rows + ROW_COUNT();
END WHILE;
DELETE FROM reached WHERE node_id = root;
SELECT * FROM reached;
DROP TABLE reached;
END