Posted on 2008-06-08 16:33
G_G 閱讀(2773)
評論(2) 編輯 收藏 所屬分類:
Database
就用數據
數據庫表地址數據(中國地區)來說吧
存儲過程:DELIMITER?//
drop?procedure?if?exists??useCursor?//
create?temporary?table?if?not?exists??aop.tmp_table(data?bigint(20))//
//建立存儲過程
CREATE?PROCEDURE?useCursor(iid?bigint(20))
????BEGIN
??? ?? ? //局部變量定義
?????????declare?tid?bigint(20)?default?-1?;
????????
??? ?? ? //游標定義
?????????declare?cur1?CURSOR?FOR?select?id?from?aop.location?where?fid=iid?;
???????? //游標介紹定義
???????? declare?CONTINUE?HANDLER?FOR?SQLSTATE?'02000'?SET?tid?=?null;
????????
???????? //開游標
???????? OPEN?cur1;
?????? ? FETCH?cur1?INTO?tid;
???????? WHILE?(?tid?is?not?null?)?
???????? DO
??????????insert?into?aop.tmp_table?values(tid);
??? ?? ?? //樹形結構數據遞歸收集到建立的臨時表中
??????????call?useCursor(tid);
????????? FETCH?cur1?INTO?tid?;
???????END?WHILE;
????END;//
DELIMITER?;
//查詢開始 ,運行是成功的,但用時有10多秒之多,才幾百條數據;
//望那個大牛 幫幫解決下時間問題!
call?useCursor(1);
select?*?from?tmp_table?;
drop?temporary?table?if??exists??aop.tmp_table?;
結果:


|??187?|
|??188?|
|??189?|
|??190?|
|??191?|
|??192?|
|??193?|
|??194?|
|??195?|
|??196?|
|??197?|
|??198?|
|??199?|
|??200?|
|??201?|
|??202?|
|??203?|
|??204?|
|??205?|
|??206?|
|??207?|
|??208?|
|??209?|

.