2015-06-11 63 views
2

我試圖從視圖訪問數據並將其插入到存儲過程中的臨時表中,下面是存儲過程。 當我嘗試執行sp時,我得到「準備好的語句需要重新準備」錯誤。準備好的語句需要在mysql中重新編寫

有人可以建議如何解決這個錯誤在MySQL中。

步驟:

DROP PROCEDURE IF EXISTS getFilteredData; 

CREATE PROCEDURE getFilteredData() 
BEGIN 

DECLARE sql2 VARCHAR(5000); 
DROP TEMPORARY TABLE IF EXISTS TempTbl; 
CREATE TEMPORARY TABLE TempTbl 
(
ID VARCHAR(50), 
Name VARCHAR(500), 
Status INT 
); 
SET sql2 = 'INSERT INTO TempTbl Select ID,Name,Id as Status from  v_release;'; 

SET @SWV_Stmt = sql2; 
PREPARE SWT_Stmt FROM @SWV_Stmt; 
EXECUTE SWT_Stmt; 
DEALLOCATE PREPARE SWT_Stmt; 

SELECT * FROM TempTbl; 
END; 

回答

0

我無法重現您報告問題。

mysql> SELECT VERSION(); 
+-----------------+ 
| VERSION()  | 
+-----------------+ 
| 5.5.22-0ubuntu1 | 
+-----------------+ 
1 row in set (0.00 sec) 

mysql> DELIMITER // 

mysql> DROP TABLE IF EXISTS `release`// 
Query OK, 0 rows affected (0.00 sec) 

mysql> DROP VIEW IF EXISTS `v_release`// 
Query OK, 0 rows affected (0.00 sec) 

mysql> DROP PROCEDURE IF EXISTS `getFilteredData`// 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE `release` (
    -> `id` VARCHAR(50), 
    -> `name` VARCHAR(500), 
    -> `status` INT 
    ->)// 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE VIEW `v_release` AS 
    -> SELECT `id`, `name`, `status` 
    -> FROM `release`// 
Query OK, 0 rows affected (0.00 sec) 

mysql> INSERT INTO `release` 
    -> (`id`, `name`, `status`) 
    -> VALUES 
    -> ('1', 'NAME1', 1), 
    -> ('2', 'NAME2', 2), 
    -> ('3', 'NAME1', 3)// 
Query OK, 3 rows affected (0.00 sec) 
Records: 3 Duplicates: 0 Warnings: 0 

mysql> CREATE PROCEDURE `getFilteredData`() 
    -> BEGIN 
    -> DROP TEMPORARY TABLE IF EXISTS `temptbl`; 
    -> CREATE TEMPORARY TABLE `temptbl` (
    ->  `id` VARCHAR(50), 
    ->  `name` VARCHAR(500), 
    ->  `status` INT); 
    -> PREPARE `stmt` FROM 'INSERT INTO `temptbl` 
    '>      SELECT `id`, `name`, `status` 
    '>      FROM `v_release`'; 
    -> EXECUTE `stmt`; 
    -> DEALLOCATE PREPARE `stmt`; 
    -> SELECT `id`, `name`, `status` 
    -> FROM `temptbl`; 
    -> DROP TEMPORARY TABLE IF EXISTS `temptbl`; 
    -> END// 
Query OK, 0 rows affected (0.00 sec) 

mysql> DELIMITER ; 

mysql> CALL `getFilteredData`; 
+------+-------+--------+ 
| id | name | status | 
+------+-------+--------+ 
| 1 | NAME1 |  1 | 
| 2 | NAME2 |  2 | 
| 3 | NAME1 |  3 | 
+------+-------+--------+ 
3 rows in set (0.00 sec) 

Query OK, 0 rows affected (0.00 sec) 

SQL Fiddle demo

+0

wchiquito您好,感謝當我嘗試運行其中U蟾蜍給MySQL的腳本的答覆,我米得到同樣的錯誤「準備語句需要重新編寫」 ,但是當我在SQL小提琴中運行這個腳本時,它運行的很好。可能是什麼問題? –

+0

@Ravikumar_n:你使用的是什麼版本的MySQL?請檢查:[Bug#42041準備好的語句在加載MySQL服務器時失敗](http://bugs.mysql.com/bug.php?id=42041)和[13.5.4自動準備語句代表](http:/ /dev.mysql.com/doc/refman/5.5/en/statement-repreparation.html)。 – wchiquito

相關問題