0
我有一個使用臨時表的存儲過程,以便我可以總結某個產品類別內所有產品的銷售情況。當我試圖運行代碼失敗時。我搜索谷歌和這裏在stackoverflow,但無法找到我做錯了什麼。我在Windows Server上使用MySQL服務器5.5。MySQL中的臨時表定義
CREATE PROCEDURE `getStatistics`(IN `startDate` date,IN `endDate` date,IN `categoryName` varchar)
BEGIN
CREATE TEMPORARY TABLE procResult(productName VARCHAR, amount INT);
CREATE TEMPORARY TABLE tblProductID(SELECT ID, `name` FROM product WHERE categoryID = (SELECT ID FROM category WHERE `name` = categoryName));
DECLARE done_amt, done_PID INT DEFAULT FALSE;
DECLARE amount, productID INT DEFAULT 0;
DECLARE pidCursor CURSOR FOR SELECT ID, `name` FROM tblProductID;
DECLARE amtCursor CURSOR FOR SELECT orderlines.amount FROM orderlines WHERE orderlines.productID = productID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_amt = TRUE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_PID = TRUE;
OPEN pidCursor;
pid_loop:LOOP
DECLARE productName VARCHAR;
FETCH pidCursor INTO productID, productName;
IF done_PID THEN
LEAVE pid_LOOP;
END IF;
OPEN amtCursor;
amt_loop:LOOP
DECLARE tmpAmount INT DEFAULT 0;
FETCH amtCursor INTO tmpAmount;
IF done_amt THEN
LEAVE amt_loop;
END IF;
amount = amount + tmpAmount;
END LOOP;
CLOSE amtCursor;
IF amount > 0 THEN
INSERT INTO procResult VALUES (productName, amount);
amount = 0;
END IF;
END LOOP;
CLOSE pidCursor;
END;
你得到了什麼錯誤?如果您在第一次結束之前運行此過程兩次,那麼您可能無法創建臨時表;) – cichy
我收到以下錯誤: '1064 - 您的SQL語法出錯;請檢查與您的MySQL服務器版本相對應的手冊,以找到在''附近使用的正確語法'' BEGIN CREATE TEMPORARY TABLE procResult(productName VARCHAR,amount INT); 'at line 1' – Feanaro
那麼它嘗試創建過程失敗。但你其實是對的...... – Feanaro