Thursday, September 25, 2014

Tables in parent child hierarchy

with
tmp_hier1 as
   ((select table_name as child, null as parent from user_tables
    minus
    select distinct table_name as child, null as parent from user_constraints where constraint_type = 'R')
   union
   select distinct table_name as child, (select distinct table_name from user_constraints ac1 where ac1.constraint_name=ac.r_constraint_name) as parent
   from user_constraints ac where constraint_type = 'R'),
tmp_hier2 as
   (select child, parent, LEVEL as lvl
   from tmp_hier1
   START WITH parent is null
   connect by nocycle prior child = parent
   order by lvl, child)
select child, max(lvl)
from tmp_hier2
group by child
order by max(lvl), child;

Thursday, January 9, 2014

Collections and records

-- VARAAY type and TABLE type.
-- VARAAY  has max length but TABLE does not have max length constraint.
-- Both are type of record and stored based on index.

declare

type varr_var is varray(10) of integer;
type tab_var is table of integer;
t_vrr tab_var;
v_arr varr_var;
cnt integer;

begin
v_arr:=varr_var(10,11,12,13,14,15,16,17,18);
cnt:=v_arr.count;
for i in 1..cnt loop
dbms_output.put_line(v_arr(i));
end loop;
t_vrr:=tab_var(30,31,32,33,34,35,36,37,38,39,40,41,42,53,45,23,37);
cnt:=t_vrr.count;
for i in 1..cnt loop
dbms_output.put_line(t_vrr(i));
--dbms_output.put_line('Anand   '||i||cnt);
end loop;
end;
/