SlideShare a Scribd company logo
1 of 161
Download to read offline
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 diaRafael 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 JavaRafael 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 automatizadossRafael Ponte
 
Greenbar - Testes automatizados na sua empresa
Greenbar - Testes automatizados na sua empresaGreenbar - Testes automatizados na sua empresa
Greenbar - Testes automatizados na sua empresaRafael Ponte
 
Desafios de um desenvolvedor JSF
Desafios de um desenvolvedor JSFDesafios de um desenvolvedor JSF
Desafios de um desenvolvedor JSFRafael 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 JSFRafael 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 produtividadeRafael Ponte
 
Entendendo Domain-Driven Design
Entendendo Domain-Driven DesignEntendendo Domain-Driven Design
Entendendo Domain-Driven DesignRafael Ponte
 
Anatomia do JSF, JavaServer Faces
Anatomia do JSF, JavaServer FacesAnatomia do JSF, JavaServer Faces
Anatomia do JSF, JavaServer FacesRafael 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

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 

Recently uploaded (20)

Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 

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