declare
      li_deptno emp.deptno%type;
      type cursor_type is ref cursor;
      cur_yy cursor_type;
  cur_xx cursor_type;
      lr_emp t_sys_orgnization%rowtype;
  orgrow t_sys_orgnization%rowtype;
      ls_table varchar2(100);
  strtable varchar2(300);
      strsql varchar2(300);
      tablesql varchar2(300);
   begin
    strsql:='select id,father from t_sys_orgnization where type=';
    tablesql := concat(strsql,to_char('p'));
    open cur_xx for tablesql;
loop
        fetch cur_xx into lr_emp;
exit when cur_xx%notfound;
     ls_table:='select *  form t_sys_orgnization where id in (';
     strtable := concat(ls_table,to_char(lr_emp.father+')'));
     open cur_yy for strtable;
     loop
        fetch cur_yy into orgrow;
exit when cur_yy%notfound;
begin
       if orgrow.type='D' then
  update t_sys_orgnization set company_id=orgrow.father where id=lr_emp.id
  end if;
if orgrow.type='C' then
  update t_sys_orgnization set company_id=orgrow.id where id=lr_emp.id
  end if; 
end;
end loop;
 close cur_yy;
    end loop;
      close cur_xx; 
  end;