2011-11-03 65 views
2

我試圖使用MySQL存儲過程調用我的遊標聲明的一部分(嵌套程序,並能夠查看結果):MySQL的:遊標聲明中過程調用

declare myCursor cursor for call MyProcedure(param1,param2,param3); 

當我這樣做雖然,我得到1064錯誤:你的SQL語法有錯誤;檢查與您的MySQL服務器版本對應的手冊,以獲取使用的正確語法。

任何人都知道如何做到這一點?

謝謝

回答

2

http://dev.mysql.com/doc/refman/5.5/en/declare-cursor.html

說:

DECLARE cursor_name CURSOR FOR select_statement

call不是select_statement中
這就是爲什麼你會收到錯誤。

解決方法
如果你使用一個存儲過程返回一個結果,使用等效的選擇語句。

問題
的問題是,一個call可以返回0,1或更多結果集。
遊標只能處理1個結果集大小寫,而AFAIK MySQL無法確切知道該調用將返回多少個結果集。

+0

啊,是的。就是這個。而且,無法從select語句調用過程。謝謝! – user1028405

+0

您可以從select語句調用**函數**。並且像那樣作弊。函數返回單個值,但不是結果集。 – Johan

0

這是我如何做的:

有過程返回臨時表中的結果集。

create procedure GetPeople(in unitId bigint(20)) 
begin 

    create temporary table if not exists GetPeople_result like People; 
    truncate table GetPeople_result; 

    insert into GetPeople_result select * from 
     -- your select here 
    ; 
end 

將光標作爲臨時表。

create procedure DoSomethingWithAllPeople(in unitId bigint(20)) 
begin 

    declare allPeopleCursor cursor for select PersonId from GetPeople_result; -- select the result from GetPeople 
    call GetPeople(unitId); -- create the GetPeople_result table and fill it 

    -- iterate cursor 

end