我正在使用PDO調用以DROP TABLE IF EXISTS開頭的存儲過程。我隨機得到PDOException'SQLSTATE [42S02]:未找到基本表或視圖:1146表'historygr.reached'不存在',甚至更煩人它會告訴我,拋出一個異常說,表已經存在,幾秒鐘之內,似乎從相同的連接。PDOException與DROP TABLE IF EXISTS
我不能自己觸發錯誤,但我得到它的錯誤通知。
下面是錯誤的起源於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
遺憾的是不能使用一個臨時表,因爲你不能引用他們不止一次在查詢這是何等的程序功能 –
呀,你會需要創建一個臨時表的副本來多次引用它(每個引用n-1個副本),或者更改您的查詢,而不是多次引用臨時表。 –