SlideShare a Scribd company logo
10 features do Oracle
que você não conhecia
#1 CONNECT BY CLAUSE
TechDay 2019 - @rponte
describe tb_funcionarios;
Name Null? Type
---------- -------- -------------
ID NOT NULL NUMBER(38)
NOME NOT NULL VARCHAR2(200)
CARGO NOT NULL VARCHAR2(100)
GERENTE_ID NUMBER(38)
describe tb_funcionarios;
Name Null? Type
---------- -------- -------------
ID NOT NULL NUMBER(38)
NOME NOT NULL VARCHAR2(200)
CARGO NOT NULL VARCHAR2(100)
GERENTE_ID NUMBER(38)
describe tb_funcionarios;
Name Null? Type
---------- -------- -------------
ID NOT NULL NUMBER(38)
NOME NOT NULL VARCHAR2(200)
CARGO NOT NULL VARCHAR2(100)
GERENTE_ID NUMBER(38)
describe tb_funcionarios;
(FOREIGN KEY)
Name Null? Type
---------- -------- -------------
ID NOT NULL NUMBER(38)
NOME NOT NULL VARCHAR2(200)
CARGO NOT NULL VARCHAR2(100)
GERENTE_ID NUMBER(38)
describe tb_funcionarios;
(FOREIGN KEY)
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
Hierarquia
Sabe aquele relatório?
select f.nome as funcionario
from tb_funcionarios f
order by f.nome
;
select f.nome as funcionario
from tb_funcionarios f
order by f.nome
;
E o gerente?
select f1.nome as funcionario
,f2.nome as gerente
from tb_funcionarios f1
,tb_funcionarios f2
where f1.gerente_id = f2.id
order by f.nome
;
select f1.nome as funcionario
,f2.nome as gerente
from tb_funcionarios f1
,tb_funcionarios f2
where f1.gerente_id = f2.id
order by f.nome
;
E o gerente do
gerente?
select f1.nome as funcionario
,f2.nome as gerente
,f3.nome as gerente_do_gerente
from tb_funcionarios f1
,tb_funcionarios f2
,tb_funcionarios f3
where f1.gerente_id = f2.id
and f2.gerente_id = f3.id
order by f.nome
;
select f1.nome as funcionario
,f2.nome as gerente
,f3.nome as gerente_do_gerente
from tb_funcionarios f1
,tb_funcionarios f2
,tb_funcionarios f3
where f1.gerente_id = f2.id
and f2.gerente_id = f3.id
order by f.nome
;
E o gerente do gerente
do gerente … do
gerente?
Qual a profundidade
dessa hierarquia?
Ou faz na aplicação,
né?
CONNECT BY CLAUSE
CONNECT BY CLAUSE
Oracle
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Stevens
Thaio
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Stevens
Thaio
Principe Victor Aldeir
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Stevens
Thaio
Principe Victor Aldeir
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Stevens
Thaio
Principe Victor Aldeir
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Stevens
Thaio
Principe Victor Aldeir
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Stevens
Thaio
Principe Victor Aldeir
PRIOR(acessando informações do parent-node)
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,????? as gerente_nome
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,????? as gerente_nome
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,prior f.nome as gerente_nome
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,prior f.nome as gerente_nome
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,prior f.nome as gerente_nome
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Mas tem algo
estranho…
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Stevens?
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
Root Node
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
;
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
Root Node
Mas como chego na
raiz?
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
Então vamos começar
a hierarquia por ele…
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
E se houver muitas
raizes?
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id = 3
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id in (3, 45, 104)
;
Mas o ID vem de uma
sequence, e agora?
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.*
from tb_funcionarios f
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.id in (3, 45, 104)
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
Como visualizar melhor essa
hierarquia?
Profundidade
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
1
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
1
2
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
1
2
3
Chris
Rafael
Plauto
Ricardo
Stevens
Thaio
Principe
Guto …
Victor Aldeir
Maia
Pimentel Roberto
…
1
2
3
4
5
6
Profundidade

(level)
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
Stevens
Thaio
Principe Victor Aldeir
Stevens
Thaio
Principe Victor Aldeir
.Stevens
..Thaio
...Principe
...Victor
...Aldeir
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || id as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.id as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.id as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.id as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.id as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || id as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
;
Ordenando…
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order by f.nome
;
????
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
x
Outra forma de
apresentar...

(path)
Outra forma de
apresentar...

(path)
Chris
Chris->Ricardo
Chris->Ricardo->Plauto
Chris->Ricardo->Plauto->Stevens
Chris->Ricardo->Plauto->Stevens->Thaio
Chris->Ricardo->Plauto->Stevens->Thaio->Aldeir
Chris->Ricardo->Plauto->Stevens->Thaio->Principe
Chris->Ricardo->Plauto->Stevens->Thaio->Victor
Chris->Ricardo->Plauto->Stevens->Thaio->…
Chris->Ricardo->Plauto->Guto
…
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
Loop infinito?
UPDATE tb_funcionarios
SET gerente_id = 1 -- Plauto
WHERE id = 3 -- Chris
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
ERROR:
ORA-01436: CONNECT BY loop in user data
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_cycle as is_cycle
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_iscycle as is_cycle
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_cycle as is_cycle
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_cycle as is_cycle
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id = 3
order siblings by f.nome
;
Tem mais?
UPDATE tb_funcionarios
SET gerente_id = null
WHERE id = 3 -- Chris
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_iscycle as is_cycle

,connect_by_isleaf as is_leaf
,connect_by_root f.id as root_id
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_iscycle as is_cycle

,connect_by_isleaf as is_leaf
,connect_by_root f.id as root_id
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_iscycle as is_cycle

,connect_by_isleaf as is_leaf
,connect_by_root f.id as root_id
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_iscycle as is_cycle

,connect_by_isleaf as is_leaf
,connect_by_root f.id as root_id
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
select f.id as id
,f.nome as funcionario
,f.cargo as cargo
,f.gerente_id as gerente_id
,level
,lpad('.', (level-1), '.') || f.nome as tree
,sys_connect_by_path(f.nome, '->') as path
,connect_by_iscycle as is_cycle

,connect_by_isleaf as is_leaf
,connect_by_root f.id as root_id
from tb_funcionarios f
connect by nocycle f.gerente_id = prior f.id
start with f.gerente_id is null
order siblings by f.nome
;
Dá pra fazer mais?
SELECT level
FROM DUAL
CONNECT BY level <= 100
;
SELECT level
FROM DUAL
CONNECT BY level <= 100
;
SELECT level
FROM DUAL
CONNECT BY level <= 100
;
INSERT INTO t(seq)
SELECT level
FROM DUAL
CONNECT BY level <= 100
;
INSERT INTO tb(seq)
SELECT level
FROM DUAL
CONNECT BY level <= 100
;
SELECT
add_months(input, (level-1) * 12) as start_date
,add_months(input, level * 12) as end_date
FROM (
-- Set the input date here
SELECT DATE'2012-02-01' as input
FROM DUAL
)
CONNECT BY
add_months(input, (level-1) * 12) < Sysdate
;
SELECT
add_months(input, (level-1) * 12) as start_date
,add_months(input, level * 12) as end_date
FROM (
-- Set the input date here
SELECT DATE'2012-02-01' as input
FROM DUAL
)
CONNECT BY
add_months(input, (level-1) * 12) < Sysdate
;
SELECT
add_months(input, (level-1) * 12) as start_date
,add_months(input, level * 12) as end_date
FROM (
-- Set the input date here
SELECT DATE'2012-02-01' as input
FROM DUAL
)
CONNECT BY
add_months(input, (level-1) * 12) < Sysdate
;
SELECT
add_months(input, (level-1) * 12) as start_date
,add_months(input, level * 12) as end_date
FROM (
-- Set the input date here
SELECT DATE'2012-02-01' as input
FROM DUAL
)
CONNECT BY
add_months(input, (level-1) * 12) < Sysdate
;
SELECT
add_months(input, (level-1) * 12) as start_date
,add_months(input, level * 12) as end_date
FROM (
-- Set the input date here
SELECT DATE'2012-02-01' as input
FROM DUAL
)
CONNECT BY
add_months(input, (level-1) * 12) < Sysdate
;
x
está incluindo hoje,
2019-01-31
Concluindo
Obrigado!
TechDay 2019 - @rponte

More Related Content

More from Rafael Ponte

Hibernate efetivo (COALTI-2014 / ALJUG)
Hibernate efetivo (COALTI-2014 / ALJUG)Hibernate efetivo (COALTI-2014 / ALJUG)
Hibernate efetivo (COALTI-2014 / ALJUG)
Rafael Ponte
 
Migrations for Java (QCONSP2013)
Migrations for Java (QCONSP2013)Migrations for Java (QCONSP2013)
Migrations for Java (QCONSP2013)
Rafael Ponte
 
Importancia dos Testes Automatizados no dia a dia (Don't Panic)
Importancia dos Testes Automatizados no dia a dia (Don't Panic)Importancia dos Testes Automatizados no dia a dia (Don't Panic)
Importancia dos Testes Automatizados no dia a dia (Don't Panic)
Rafael Ponte
 
Importância dos testes automatizados no dia a dia
Importância dos testes automatizados no dia a diaImportância dos testes automatizados no dia a dia
Importância dos testes automatizados no dia a dia
Rafael Ponte
 
Hibernate Efetivo (QCONSP-2012)
Hibernate Efetivo (QCONSP-2012)Hibernate Efetivo (QCONSP-2012)
Hibernate Efetivo (QCONSP-2012)
Rafael Ponte
 
Migrations for Java
Migrations for JavaMigrations for Java
Migrations for Java
Rafael Ponte
 
Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)
Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)
Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)
Rafael Ponte
 
Importância dos testes automatizadoss
Importância dos testes automatizadossImportância dos testes automatizadoss
Importância dos testes automatizadoss
Rafael Ponte
 
Greenbar - Testes automatizados na sua empresa
Greenbar - Testes automatizados na sua empresaGreenbar - Testes automatizados na sua empresa
Greenbar - Testes automatizados na sua empresa
Rafael Ponte
 
Desafios de um desenvolvedor JSF
Desafios de um desenvolvedor JSFDesafios de um desenvolvedor JSF
Desafios de um desenvolvedor JSF
Rafael Ponte
 
Curso de Java server faces (JSF)
Curso de Java server faces (JSF)Curso de Java server faces (JSF)
Curso de Java server faces (JSF)
Rafael Ponte
 
Os 10 maus hábitos dos desenvolvedores JSF
Os 10 maus hábitos dos desenvolvedores JSFOs 10 maus hábitos dos desenvolvedores JSF
Os 10 maus hábitos dos desenvolvedores JSF
Rafael Ponte
 
Boas Práticas com JavaServer Faces (Jsf)
Boas Práticas com JavaServer Faces (Jsf)Boas Práticas com JavaServer Faces (Jsf)
Boas Práticas com JavaServer Faces (Jsf)
Rafael Ponte
 
JavaServer Faces - Desenvolvendo aplicações web com produtividade
JavaServer Faces - Desenvolvendo aplicações web com produtividadeJavaServer Faces - Desenvolvendo aplicações web com produtividade
JavaServer Faces - Desenvolvendo aplicações web com produtividade
Rafael Ponte
 
Entendendo Domain-Driven Design
Entendendo Domain-Driven DesignEntendendo Domain-Driven Design
Entendendo Domain-Driven Design
Rafael Ponte
 
Anatomia do JSF, JavaServer Faces
Anatomia do JSF, JavaServer FacesAnatomia do JSF, JavaServer Faces
Anatomia do JSF, JavaServer Faces
Rafael Ponte
 

More from Rafael Ponte (16)

Hibernate efetivo (COALTI-2014 / ALJUG)
Hibernate efetivo (COALTI-2014 / ALJUG)Hibernate efetivo (COALTI-2014 / ALJUG)
Hibernate efetivo (COALTI-2014 / ALJUG)
 
Migrations for Java (QCONSP2013)
Migrations for Java (QCONSP2013)Migrations for Java (QCONSP2013)
Migrations for Java (QCONSP2013)
 
Importancia dos Testes Automatizados no dia a dia (Don't Panic)
Importancia dos Testes Automatizados no dia a dia (Don't Panic)Importancia dos Testes Automatizados no dia a dia (Don't Panic)
Importancia dos Testes Automatizados no dia a dia (Don't Panic)
 
Importância dos testes automatizados no dia a dia
Importância dos testes automatizados no dia a diaImportância dos testes automatizados no dia a dia
Importância dos testes automatizados no dia a dia
 
Hibernate Efetivo (QCONSP-2012)
Hibernate Efetivo (QCONSP-2012)Hibernate Efetivo (QCONSP-2012)
Hibernate Efetivo (QCONSP-2012)
 
Migrations for Java
Migrations for JavaMigrations for Java
Migrations for Java
 
Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)
Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)
Os 10 maus habitos dos desenvolvedores jsf (JustJava e CCT)
 
Importância dos testes automatizadoss
Importância dos testes automatizadossImportância dos testes automatizadoss
Importância dos testes automatizadoss
 
Greenbar - Testes automatizados na sua empresa
Greenbar - Testes automatizados na sua empresaGreenbar - Testes automatizados na sua empresa
Greenbar - Testes automatizados na sua empresa
 
Desafios de um desenvolvedor JSF
Desafios de um desenvolvedor JSFDesafios de um desenvolvedor JSF
Desafios de um desenvolvedor JSF
 
Curso de Java server faces (JSF)
Curso de Java server faces (JSF)Curso de Java server faces (JSF)
Curso de Java server faces (JSF)
 
Os 10 maus hábitos dos desenvolvedores JSF
Os 10 maus hábitos dos desenvolvedores JSFOs 10 maus hábitos dos desenvolvedores JSF
Os 10 maus hábitos dos desenvolvedores JSF
 
Boas Práticas com JavaServer Faces (Jsf)
Boas Práticas com JavaServer Faces (Jsf)Boas Práticas com JavaServer Faces (Jsf)
Boas Práticas com JavaServer Faces (Jsf)
 
JavaServer Faces - Desenvolvendo aplicações web com produtividade
JavaServer Faces - Desenvolvendo aplicações web com produtividadeJavaServer Faces - Desenvolvendo aplicações web com produtividade
JavaServer Faces - Desenvolvendo aplicações web com produtividade
 
Entendendo Domain-Driven Design
Entendendo Domain-Driven DesignEntendendo Domain-Driven Design
Entendendo Domain-Driven Design
 
Anatomia do JSF, JavaServer Faces
Anatomia do JSF, JavaServer FacesAnatomia do JSF, JavaServer Faces
Anatomia do JSF, JavaServer Faces
 

Recently uploaded

Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
marufrahmanstratejm
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 

Recently uploaded (20)

Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 

TechDay: 10 Features do Oracle que voce nao conhecia - CONNECT BY CLAUSE

  • 1. 10 features do Oracle que você não conhecia #1 CONNECT BY CLAUSE TechDay 2019 - @rponte
  • 3. Name Null? Type ---------- -------- ------------- ID NOT NULL NUMBER(38) NOME NOT NULL VARCHAR2(200) CARGO NOT NULL VARCHAR2(100) GERENTE_ID NUMBER(38) describe tb_funcionarios;
  • 4. Name Null? Type ---------- -------- ------------- ID NOT NULL NUMBER(38) NOME NOT NULL VARCHAR2(200) CARGO NOT NULL VARCHAR2(100) GERENTE_ID NUMBER(38) describe tb_funcionarios;
  • 5. Name Null? Type ---------- -------- ------------- ID NOT NULL NUMBER(38) NOME NOT NULL VARCHAR2(200) CARGO NOT NULL VARCHAR2(100) GERENTE_ID NUMBER(38) describe tb_funcionarios; (FOREIGN KEY)
  • 6. Name Null? Type ---------- -------- ------------- ID NOT NULL NUMBER(38) NOME NOT NULL VARCHAR2(200) CARGO NOT NULL VARCHAR2(100) GERENTE_ID NUMBER(38) describe tb_funcionarios; (FOREIGN KEY)
  • 19. select f.nome as funcionario from tb_funcionarios f order by f.nome ;
  • 20. select f.nome as funcionario from tb_funcionarios f order by f.nome ;
  • 22. select f1.nome as funcionario ,f2.nome as gerente from tb_funcionarios f1 ,tb_funcionarios f2 where f1.gerente_id = f2.id order by f.nome ;
  • 23. select f1.nome as funcionario ,f2.nome as gerente from tb_funcionarios f1 ,tb_funcionarios f2 where f1.gerente_id = f2.id order by f.nome ;
  • 24. E o gerente do gerente?
  • 25. select f1.nome as funcionario ,f2.nome as gerente ,f3.nome as gerente_do_gerente from tb_funcionarios f1 ,tb_funcionarios f2 ,tb_funcionarios f3 where f1.gerente_id = f2.id and f2.gerente_id = f3.id order by f.nome ;
  • 26. select f1.nome as funcionario ,f2.nome as gerente ,f3.nome as gerente_do_gerente from tb_funcionarios f1 ,tb_funcionarios f2 ,tb_funcionarios f3 where f1.gerente_id = f2.id and f2.gerente_id = f3.id order by f.nome ;
  • 27. E o gerente do gerente do gerente … do gerente?
  • 29. Ou faz na aplicação, né?
  • 30.
  • 33. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 34. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 35. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 36. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 37. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 38. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 39. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 40. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 41. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 42. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Stevens Thaio
  • 43. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 44. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Stevens Thaio Principe Victor Aldeir
  • 45. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Stevens Thaio Principe Victor Aldeir
  • 46. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Stevens Thaio Principe Victor Aldeir
  • 47. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Stevens Thaio Principe Victor Aldeir
  • 48. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Stevens Thaio Principe Victor Aldeir
  • 49.
  • 51. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,????? as gerente_nome from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 52. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,????? as gerente_nome from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 53. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,prior f.nome as gerente_nome from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 54. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,prior f.nome as gerente_nome from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 55. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,prior f.nome as gerente_nome from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 57. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ;
  • 59. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Chris Rafael Plauto Ricardo Stevens Thaio Principe Guto … Victor Aldeir Maia Pimentel Roberto …
  • 60. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Chris Rafael Plauto Ricardo Stevens Thaio Principe Guto … Victor Aldeir Maia Pimentel Roberto …
  • 61. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Chris Rafael Plauto Ricardo Stevens Thaio Principe Guto … Victor Aldeir Maia Pimentel Roberto …
  • 62. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Chris Rafael Plauto Ricardo Stevens Thaio Principe Guto … Victor Aldeir Maia Pimentel Roberto … Root Node
  • 63. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id ; Chris Rafael Plauto Ricardo Stevens Thaio Principe Guto … Victor Aldeir Maia Pimentel Roberto … Root Node
  • 64. Mas como chego na raiz?
  • 69. Então vamos começar a hierarquia por ele…
  • 70. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 71. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 72. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 73. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 74. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 75. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 76. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 77. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 78. E se houver muitas raizes?
  • 79. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id = 3 ;
  • 80. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id in (3, 45, 104) ;
  • 81. Mas o ID vem de uma sequence, e agora?
  • 86. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.id in (3, 45, 104) ;
  • 87. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 88. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 89. Como visualizar melhor essa hierarquia?
  • 97. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 98. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 99. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 100. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 103. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || id as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 104. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.id as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 105. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.id as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 106. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.id as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 107. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.id as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 108. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || id as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 109. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 110. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null ;
  • 112. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 113. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order by f.nome ;
  • 114. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order by f.nome ;
  • 115. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order by f.nome ; ????
  • 116. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order by f.nome ;
  • 117. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order by f.nome ;
  • 118. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 119. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 120. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ; x
  • 123. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 124. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 125. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 126. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 127. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 128. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 130. UPDATE tb_funcionarios SET gerente_id = 1 -- Plauto WHERE id = 3 -- Chris ;
  • 131. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 132. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 133. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ;
  • 134. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ; ERROR: ORA-01436: CONNECT BY loop in user data
  • 135. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ;
  • 136. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ;
  • 137. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 138. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_cycle as is_cycle from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ;
  • 139. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_iscycle as is_cycle from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ;
  • 140. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_cycle as is_cycle from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ;
  • 141. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_cycle as is_cycle from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id = 3 order siblings by f.nome ;
  • 143. UPDATE tb_funcionarios SET gerente_id = null WHERE id = 3 -- Chris ;
  • 144. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_iscycle as is_cycle
 ,connect_by_isleaf as is_leaf ,connect_by_root f.id as root_id from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 145. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_iscycle as is_cycle
 ,connect_by_isleaf as is_leaf ,connect_by_root f.id as root_id from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 146. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_iscycle as is_cycle
 ,connect_by_isleaf as is_leaf ,connect_by_root f.id as root_id from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 147. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_iscycle as is_cycle
 ,connect_by_isleaf as is_leaf ,connect_by_root f.id as root_id from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 148. select f.id as id ,f.nome as funcionario ,f.cargo as cargo ,f.gerente_id as gerente_id ,level ,lpad('.', (level-1), '.') || f.nome as tree ,sys_connect_by_path(f.nome, '->') as path ,connect_by_iscycle as is_cycle
 ,connect_by_isleaf as is_leaf ,connect_by_root f.id as root_id from tb_funcionarios f connect by nocycle f.gerente_id = prior f.id start with f.gerente_id is null order siblings by f.nome ;
  • 149. Dá pra fazer mais?
  • 150. SELECT level FROM DUAL CONNECT BY level <= 100 ;
  • 151. SELECT level FROM DUAL CONNECT BY level <= 100 ;
  • 152. SELECT level FROM DUAL CONNECT BY level <= 100 ;
  • 153. INSERT INTO t(seq) SELECT level FROM DUAL CONNECT BY level <= 100 ;
  • 154. INSERT INTO tb(seq) SELECT level FROM DUAL CONNECT BY level <= 100 ;
  • 155. SELECT add_months(input, (level-1) * 12) as start_date ,add_months(input, level * 12) as end_date FROM ( -- Set the input date here SELECT DATE'2012-02-01' as input FROM DUAL ) CONNECT BY add_months(input, (level-1) * 12) < Sysdate ;
  • 156. SELECT add_months(input, (level-1) * 12) as start_date ,add_months(input, level * 12) as end_date FROM ( -- Set the input date here SELECT DATE'2012-02-01' as input FROM DUAL ) CONNECT BY add_months(input, (level-1) * 12) < Sysdate ;
  • 157. SELECT add_months(input, (level-1) * 12) as start_date ,add_months(input, level * 12) as end_date FROM ( -- Set the input date here SELECT DATE'2012-02-01' as input FROM DUAL ) CONNECT BY add_months(input, (level-1) * 12) < Sysdate ;
  • 158. SELECT add_months(input, (level-1) * 12) as start_date ,add_months(input, level * 12) as end_date FROM ( -- Set the input date here SELECT DATE'2012-02-01' as input FROM DUAL ) CONNECT BY add_months(input, (level-1) * 12) < Sysdate ;
  • 159. SELECT add_months(input, (level-1) * 12) as start_date ,add_months(input, level * 12) as end_date FROM ( -- Set the input date here SELECT DATE'2012-02-01' as input FROM DUAL ) CONNECT BY add_months(input, (level-1) * 12) < Sysdate ; x está incluindo hoje, 2019-01-31