2013-02-12 44 views
0
DROP PROCEDURE IF EXISTS Cursor_Test;# MySQL returned an empty result set (i.e. zero rows). 

DELIMITER $$ 

CREATE PROCEDURE Cursor_Test() 
BEGIN 

    DECLARE Project_Number_val VARCHAR(255); 
    DECLARE Project_List_val VARCHAR(255); 



    DECLARE no_more_rows BOOLEAN; 
    DECLARE loop_cntr INT DEFAULT 0; 
    DECLARE num_rows INT DEFAULT 0; 


    DECLARE projects_cur CURSOR FOR 
    SELECT Project_Id 
    FROM Project_Details; 


    DECLARE CONTINUE HANDLER FOR NOT FOUND 
    SET no_more_rows = TRUE; 


    OPEN projects_cur; 
    select FOUND_ROWS() into num_rows; 

    the_loop: LOOP 

    FETCH projects_cur 
    INTO Project_Number_val; 


    IF no_more_rows THEN 
     CLOSE projects_cur; 
     LEAVE the_loop; 
    END IF; 

SET Project_List_val=CONCAT(`Project_Number_val`,'_List') 

-CONCAT沒有在MySQL的工作存儲過程

> ---> **Please check am I doing CONCAT correct here?** 


Insert Into test (Panel_Id) select Panel_Id from Project_List_val where Project_Number_val='9'; 

> --->**Is this taking 9_List as table name?** 


    SET loop_cntr = loop_cntr + 1; 
    END LOOP the_loop; 


    select num_rows, loop_cntr; 


END $$# MySQL returned an empty result set (i.e. zero rows). 


DELIMITER 

有什麼建議? 大家好,

我有一個名爲Project_Number的存儲過程中的變量,它是varchar類型。

我的要求是「爲每個項目編號查詢表project_Number_List從列表中得到的結果,並插入到另一個表」

比方說,PROJECT_NUMBER可能會像我需要這個22,21,34,43,434 相應的表查詢是這樣的22_List,21_List,34_List .....

我使用光標來循環通過Project_Number,但我的問題是如何混合project_number和_list即22_list查詢表22_List

回答

7

試試這個:

SET Project_List_val=CONCAT(Project_Number_val,'_List') 
+0

加快+1分! – silly 2013-02-12 08:18:17

+0

#1064 - 您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到在「插入到測試(Manthan_Panel_Id)」附近使用的正確語法附近的第41行的Project_List_va中選擇Manthan_Panel_Id。 – user1597811 2013-02-12 08:26:32

+0

工作正常。我沒有放;在聲明結束時。 – user1597811 2013-02-12 08:31:29

2

你VAR電話是錯誤的,試試這個:

SET Project_List_val = CONCAT(Project_Number_val,'_List') 
+0

它顯示上面的錯誤評論上面的帖子 – user1597811 2013-02-12 08:27:31

4
SET Project_List_val=CONCAT(Project_Number_val,'_List'); 

不要把單引號對於Project_Number_val vairable。