set serveroutput on
martes, 6 de septiembre de 2011
Cursor que liste de all_tables y de all_tab_columns, nombre de tabla y atributo
declare
cursor CrListaAtributos (pTabla varchar2) is
select COLUMN_NAME
from all_tab_columns
where TABLE_NAME = pTabla ;
cursor CrListatablas is
select distinct tablas.table_name
from all_tables tablas
where table_name not like '%$%'
and owner not like '%SYS%';
vNomTabla all_tables.table_name%type;
vAtributoTabla all_tab_columns.table_name%type;
begin
open CrListatablas;
loop
fetch CrListatablas into vNomTabla;
exit when CrListatablas %notfound;
dbms_output.put_line('--------------------------------');
dbms_output.put_line(vNomTabla);
dbms_output.put_line('--------------------------------');
open CrListaAtributos(vNomTabla);
loop
fetch CrListaAtributos into vAtributoTabla;
exit when CrListaAtributos%notfound;
dbms_output.put_line(vAtributoTabla);
end loop;
close CrListaAtributos;
end loop;
close CrListatablas;
end;
cursor CrListaAtributos (pTabla varchar2) is
select COLUMN_NAME
from all_tab_columns
where TABLE_NAME = pTabla ;
cursor CrListatablas is
select distinct tablas.table_name
from all_tables tablas
where table_name not like '%$%'
and owner not like '%SYS%';
vNomTabla all_tables.table_name%type;
vAtributoTabla all_tab_columns.table_name%type;
begin
open CrListatablas;
loop
fetch CrListatablas into vNomTabla;
exit when CrListatablas %notfound;
dbms_output.put_line('--------------------------------');
dbms_output.put_line(vNomTabla);
dbms_output.put_line('--------------------------------');
open CrListaAtributos(vNomTabla);
loop
fetch CrListaAtributos into vAtributoTabla;
exit when CrListaAtributos%notfound;
dbms_output.put_line(vAtributoTabla);
end loop;
close CrListaAtributos;
end loop;
close CrListatablas;
end;
lunes, 5 de septiembre de 2011
contar cuantos empleados hay por cada departamento :
Creamos un atributo en la tabla departments :
ALTER table DEPARTMENTS add TotEmplexDepartamento number(100)
Ahora procedo a realizar el cursor :
Declare cursor
miCursor is
select e.department_id, count(p.employee_id) as Empleados
from Departments e, Employees p
where p.department_id = e.department_id
group by p.department_id;
vnumdepartamento employees.employee_id%type;
vtotempleados employees.employee_id%type;
begin
open micursor;
loop
fetch micursor into vnumdepartamento,vtotempleados;
exit when micursor%notfound;
update DEPARTMENTS set TotEmplexDepartamento= vtotempleados
where department_id= vnumdepartamento
end loop;
close micursor;
End;
Creamos un atributo en la tabla departments :
ALTER table DEPARTMENTS add TotEmplexDepartamento number(100)
Ahora procedo a realizar el cursor :
Declare cursor
miCursor is
select e.department_id, count(p.employee_id) as Empleados
from Departments e, Employees p
where p.department_id = e.department_id
group by p.department_id;
vnumdepartamento employees.employee_id%type;
vtotempleados employees.employee_id%type;
begin
open micursor;
loop
fetch micursor into vnumdepartamento,vtotempleados;
exit when micursor%notfound;
update DEPARTMENTS set TotEmplexDepartamento= vtotempleados
where department_id= vnumdepartamento
end loop;
close micursor;
End;
Suscribirse a:
Entradas (Atom)