Skynet

          ---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ----------

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            112 Posts :: 1 Stories :: 49 Comments :: 0 Trackbacks

          就用數(shù)據(jù)數(shù)據(jù)庫(kù)表地址數(shù)據(jù)(中國(guó)地區(qū)) 來(lái)說(shuō)吧(用Windows 請(qǐng)使用 gbk !!)

          可直接運(yùn)行(去除注解)

          存儲(chǔ)過(guò)程:

          DELIMITER //
          drop procedure if exists  findLChild//
          /* iid 遞歸父節(jié)點(diǎn) , layer 允許遞歸深度 */

          CREATE PROCEDURE findLChild(iid bigint(20),layer bigint(20))
           
          BEGIN
             
          /*創(chuàng)建接受查詢的臨時(shí)表 */
              
          create temporary  table if not exists tmp_table(id bigint(20),name varchar(50)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
              
          /*最高允許遞歸數(shù)*/
             
          SET @@max_sp_recursion_depth = 99 ;
              call iterative(iid,layer);
          /*核心數(shù)據(jù)收集*/
             
          select * from tmp_table ;/* 展現(xiàn) */
              
          drop temporary  table if  exists  tmp_table ;/*刪除臨時(shí)表*/
            
          END;//
          DELIMITER ;

          DELIMITER 
          //
          drop procedure if exists  iterative //
          CREATE PROCEDURE iterative(iid bigint(20),layer bigint(20))
              
          BEGIN
                   
          declare tid bigint(20default -1 ;
                   
          declare tname varchar(50character set utf8;

                   
          /* 游標(biāo)定義 */
                   
          declare cur1 CURSOR FOR select id,name from location where fid=iid ;
                   
          declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tid = null;
                
                
          /* 允許遞歸深度 */
                
          if layer>0 then
                   
          OPEN cur1 ;
                   
          FETCH cur1 INTO tid,tname ;
                     
          WHILE ( tid is not null ) 
                       DO
                        
          /* 核心數(shù)據(jù)收集 */
                     
          insert into tmp_table values(tid,tname);
                        call iterative(tid,layer
          -1);
                        
          FETCH cur1 INTO tid,tname ;
                     
          END WHILE;
                 
          end if;
              
          END;//
          DELIMITER ;

          //運(yùn)行!!

          mysql> call findLChild(1,1);
          +------+------------------+
          | id   | name             |
          +------+------------------+
          |    2 | 北京             |
          |    4 | 上海             |
          |    6 | 香港特別行政區(qū)   |
          |    8 | 澳門(mén)特別行政區(qū)   |
          |   10 | 河北             |
          |   23 | 山西             |
          |   35 | 遼寧             |
          |   50 | 吉林             |
          |   60 | 黑龍江           |
          |   74 | 江蘇             |
          |   88 | 浙江             |
          |  101 | 安徽             |
          |  119 | 福建             |
          |  129 | 江西             |
          |  142 | 山東             |
          |  160 | 河南             |
          |  179 | 湖北             |
          |  198 | 湖南             |
          |  213 | 廣東             |
          |  235 | 甘肅             |
          |  250 | 四川             |
          |  272 | 貴州             |
          |  282 | 海南             |
          |  301 | 云南             |
          |  318 | 青海             |
          |  327 | 陜西             |
          |  348 | 廣西壯族自治區(qū)   |
          |  363 | 西藏自治區(qū)       |
          |  371 | 寧夏回族自治區(qū)   |
          |  377 | 新疆維吾爾自治區(qū) |
          |  400 | 內(nèi)蒙古自治區(qū)     |
          |  413 | 臺(tái)灣省           |
          +------+------------------+
          32 rows in set (0.02 sec)






          整理 www.aygfsteel.com/Good-Game
          posted on 2008-10-01 17:19 劉凱毅 閱讀(4082) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): database
          主站蜘蛛池模板: 惠安县| 玛纳斯县| 汉中市| 洪江市| 吴忠市| 黎城县| 凌海市| 类乌齐县| 巴塘县| 浦城县| 铅山县| 东安县| 淮阳县| 富锦市| 桂阳县| 寿宁县| 当涂县| 临沭县| 道孚县| 七台河市| 开封县| 枣庄市| 巫山县| 分宜县| 卓资县| 离岛区| 德江县| 增城市| 门头沟区| 安宁市| 合肥市| 柯坪县| 珠海市| 盐池县| 二连浩特市| 岳普湖县| 荣成市| 新闻| 栾城县| 沭阳县| 洪江市|