码迷,mamicode.com
首页 > 数据库 > 详细

oracle数据库中函数的递归调用

时间:2017-10-28 18:48:33      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:ber   pid   col   cti   from   lsp   记录   cell   递归函数   

如有下面的表结构AAAA,用一个字段prev_id表示记录的先后顺序,要对其排序,需要用的递归函数

ID PREV_ID CONT
99   a
23 54 d
21 23 e
54 33 c
33 99 b
32 45 g
45 21 f

 

 

 

 

 

 

如:

 create or replace function sequen(cid number)
 return number is
 pid number(2);
 begin
 select prev_id into pid from aaaa where id=cid;
  if pid is null then return 1;
  else return sequen(pid)+1; --递归调用自己
  end if;
 end;

查询语句 select sequen(id) sq, t.* from AAAA t order by sq

结果:

SQ ID PREV_ID CONT
1 99   a
2 33 99 b
3 54 33 c
4 23 54 d
5 21 23 e
6 45 21 f
7 32 45 g

oracle数据库中函数的递归调用

标签:ber   pid   col   cti   from   lsp   记录   cell   递归函数   

原文地址:http://www.cnblogs.com/mol1995/p/7747565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!