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é?
TechDay: 10 Features do Oracle que voce nao conhecia - CONNECT BY CLAUSE
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
TechDay: 10 Features do Oracle que voce nao conhecia - CONNECT BY CLAUSE
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

PDF
TechDay Retrospectiva 2018
PDF
Arquitetura Java - Escalando além do Hype
PDF
Como treinar seu estagiario
PDF
Lidando com o Caos: Testando Código PLSQL em um Projeto Critico
PDF
Como Apresentar Codigo em Slides - Javou #7 - 2016
PDF
Migrations for Java (Javou #4 - JavaCE)
PDF
Importancia dos Testes Automatizados no dia a dia FIC-Estacio 2015
PDF
Hibernate efetivo (IA-2014 / Disturbing the Mind)
TechDay Retrospectiva 2018
Arquitetura Java - Escalando além do Hype
Como treinar seu estagiario
Lidando com o Caos: Testando Código PLSQL em um Projeto Critico
Como Apresentar Codigo em Slides - Javou #7 - 2016
Migrations for Java (Javou #4 - JavaCE)
Importancia dos Testes Automatizados no dia a dia FIC-Estacio 2015
Hibernate efetivo (IA-2014 / Disturbing the Mind)

More from Rafael Ponte (16)

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

Recently uploaded (20)

PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Hybrid model detection and classification of lung cancer
PPT
What is a Computer? Input Devices /output devices
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Developing a website for English-speaking practice to English as a foreign la...
PPTX
Chapter 5: Probability Theory and Statistics
PDF
Getting Started with Data Integration: FME Form 101
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
STKI Israel Market Study 2025 version august
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
Five Habits of High-Impact Board Members
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PPTX
Tartificialntelligence_presentation.pptx
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
A novel scalable deep ensemble learning framework for big data classification...
Final SEM Unit 1 for mit wpu at pune .pptx
A review of recent deep learning applications in wood surface defect identifi...
Hybrid model detection and classification of lung cancer
What is a Computer? Input Devices /output devices
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Getting started with AI Agents and Multi-Agent Systems
Developing a website for English-speaking practice to English as a foreign la...
Chapter 5: Probability Theory and Statistics
Getting Started with Data Integration: FME Form 101
observCloud-Native Containerability and monitoring.pptx
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
STKI Israel Market Study 2025 version august
NewMind AI Weekly Chronicles – August ’25 Week III
Module 1.ppt Iot fundamentals and Architecture
Five Habits of High-Impact Board Members
Taming the Chaos: How to Turn Unstructured Data into Decisions
Tartificialntelligence_presentation.pptx
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
A novel scalable deep ensemble learning framework for big data classification...
Ad

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é?
  • 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
  • 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