Start with...Connect By子句递归查询(适合用于树状结构查询)
如:
select t.*,level lev from (select id,ref_id,name dwmc,ext1 dwdm from t_departments order by id) t start with id='2161' connect by prior id =ref_id
1
/ \
2 3
/ \ / \
4 5 6 7
start with 表示从哪个(1,2,3,4,5,6,7)节点开始 ,connect by 父主键=子外键 把它下面的所有节点都查询出来,
并依次排开,相当于从哪个节点开始取一个树下来
using 的用法
如:
v_sql:='select '||dwjb||','||dwbm||','||dwqx||',ref_id from t_departments where id=:p1';
execute immediate v_sql into v_dwjb,v_dwbm,v_dwqx,v_parent using v_units_id;
参数:固定格式,从p1开始,依次为p2,p3.......
using 则依次给参数赋值
如有参数p1,p2,p3
则using为:using _p1,_p2,_p3
假设参数都为int类型,则_p1,_p2,_p3可以分别取值为1,2,3
获取下一个序号
如:
select seq_department_id.nextval into v_id from dual;
seq_department_id为自己建立的序号
游标循环取值,及退出条件
如:
loop
fetch v_cur into v_co
exit when v_cur%notfound;