set serveroutput on
Programacion Base Datos
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;
martes, 30 de agosto de 2011
Cursor
--Cursor que permita determinar los empleados cuyo cargo sea finance manager o programmer, que tienen una renta menor o igual al promedio entre el salario minimo y maximo definido en la tabla jobs--
--Para los empleados que tiene menos del promedio estudiar aumento y para los que tengan mas estudiar carga de trabajo--
-- Generando un registro en la tabla , estudio_sueldo, donde se graben el id_empleado , nombre , apellido , renta , y diferencia con promedio--
Create table estudio_sueldo(
EMPLOYEE_ID number (6),
FIRST_NAME varchar2 (20),
LAST_NAME varchar2 (25),
SALARY number (8,2),
PROMEDIO number (8,2),
MESSAGE varchar2 (30))
Incompleto
Declare
cursor miCursor is
Select first_name , last_name, e.job_id, employee_id, salary
From Employees e, Jobs j
Where e.job_id = j.job_id
and job_title = 'Finance Manager'
or job_title = 'Programmer';
vNombre employees.first_name%type;
vApellido employees.last_name%type;
vJobid employees.job_id%type;
vEmpleadoid employees.employee_id%type;
vSalario employees.salary%type;
vSueldoMin employees.salary%type;
vSueldoMax employees.salary%type;
vPromedio number (8,2);
Begin
open miCursor;
loop
fetch miCursor into vNombre,vApellido,vJobid,vEmpleadoid,vSalario;
exit when miCursor%notfound;
dbms_output.put_line(vNombre ||' '||vApellido||' '||vJobId||' '||vEmpleadoId||' '||vSalario);
select min_salary , max_salary into vSueldoMin, vSueldoMax
from jobs
where vJobId = job_id
vPromedio = (vSueldoMin + vSueldoMax)/2;
if vSueldo >= vPromedio
then vMessage := 'evaluar carga de trabajo'
else
vMessage := 'Estudiar aumento de sueldo';
end if;
--Falta calcular la diferencia ente los sueldos y promedios--
--Realizar enunciado--
insert into estudio_sueldo values (vEmpleadoid,vNombre,vApellido,vSalario, vPromedio)
end loop;
close miCursor;
End;
--Para los empleados que tiene menos del promedio estudiar aumento y para los que tengan mas estudiar carga de trabajo--
-- Generando un registro en la tabla , estudio_sueldo, donde se graben el id_empleado , nombre , apellido , renta , y diferencia con promedio--
Create table estudio_sueldo(
EMPLOYEE_ID number (6),
FIRST_NAME varchar2 (20),
LAST_NAME varchar2 (25),
SALARY number (8,2),
PROMEDIO number (8,2),
MESSAGE varchar2 (30))
Incompleto
Declare
cursor miCursor is
Select first_name , last_name, e.job_id, employee_id, salary
From Employees e, Jobs j
Where e.job_id = j.job_id
and job_title = 'Finance Manager'
or job_title = 'Programmer';
vNombre employees.first_name%type;
vApellido employees.last_name%type;
vJobid employees.job_id%type;
vEmpleadoid employees.employee_id%type;
vSalario employees.salary%type;
vSueldoMin employees.salary%type;
vSueldoMax employees.salary%type;
vPromedio number (8,2);
Begin
open miCursor;
loop
fetch miCursor into vNombre,vApellido,vJobid,vEmpleadoid,vSalario;
exit when miCursor%notfound;
dbms_output.put_line(vNombre ||' '||vApellido||' '||vJobId||' '||vEmpleadoId||' '||vSalario);
select min_salary , max_salary into vSueldoMin, vSueldoMax
from jobs
where vJobId = job_id
vPromedio = (vSueldoMin + vSueldoMax)/2;
if vSueldo >= vPromedio
then vMessage := 'evaluar carga de trabajo'
else
vMessage := 'Estudiar aumento de sueldo';
end if;
--Falta calcular la diferencia ente los sueldos y promedios--
--Realizar enunciado--
insert into estudio_sueldo values (vEmpleadoid,vNombre,vApellido,vSalario, vPromedio)
end loop;
close miCursor;
End;
Consultas
Select first_name , last_name, e.job_id
From Employees e, Jobs j
Where e.job_id = j.job_id
and job_title = 'Finance Manager'
--(Tiene que estar igual el campo a buscar que en la tabla)--
-- Si no se realiza el lower o upper ---
Select first_name , last_name, e.job_id
From Employees e, Jobs j
Where e.job_id = j.job_id
and lower (job_title) = 'finance manager'
-- Listar todos los empleados que tienen los cargos finance manager , programmer --
Select first_name , last_name, e.job_id
From Employees e, Jobs j
Where e.job_id = j.job_id
and job_title = 'Finance Manager'
or job_title = 'Programmer'
From Employees e, Jobs j
Where e.job_id = j.job_id
and job_title = 'Finance Manager'
--(Tiene que estar igual el campo a buscar que en la tabla)--
-- Si no se realiza el lower o upper ---
Select first_name , last_name, e.job_id
From Employees e, Jobs j
Where e.job_id = j.job_id
and lower (job_title) = 'finance manager'
-- Listar todos los empleados que tienen los cargos finance manager , programmer --
Select first_name , last_name, e.job_id
From Employees e, Jobs j
Where e.job_id = j.job_id
and job_title = 'Finance Manager'
or job_title = 'Programmer'
martes, 23 de agosto de 2011
Cursor.-
select department_id as "Departamento",
count(*) as "Total Empleado"
From employees
group by department_id
select DEPARTMENT_NAME as "departamento",
count(*) as "totalEmpleados"
from DEPARTMENTS d, EMPLOYEES e
Where e.DEPARTMENT_ID = d.DEPARTMENT_ID
Group by DEPARTMENT_NAME
Seleccionar el nombre de los departamentos donde trabaja el empleado.
select DEPARTMENT_NAME ,count(EMPLOYEE_ID)
from DEPARTMENTS d, EMPLOYEES e
Where e.DEPARTMENT_ID = d.DEPARTMENT_ID
Group by DEPARTMENT_NAME
Order by DEPARTMENT_NAME
---------------------------------------------
Cursor
Declare
cursor miCursor is
select department_id ,
count(*)
From employees
group by department_id
Order by department_id;
vDepartamento employeeS.employee_id%type;
vTotEmpleado integer;
Begin
open miCursor;
dbms_output.put_line('Departamento TotEmpleado');
loop
fetch miCursor into vDepartamento, vTotEmpleado;
exit when miCursor%notfound;
dbms_output.put_line(vDepartamento || ' ' || vTotEmpleado);
end loop;
close miCursor;
End;
----------------------------------------------
Saber cual es el nombre de un codigo de un depto.
Declare
vNumeroDepto integer := 10;
vNombreDepto char(50);
begin
Select DEPARTMENT_NAME into vNombreDepto
from departments
where department_id = vNumeroDepto;
dbms_output.put_line('El codigo del depto es : ' || vNumeroDepto);
dbms_output.put_line('El codigo del depto es : ' || vNombreDepto);
end;
count(*) as "Total Empleado"
From employees
group by department_id
select DEPARTMENT_NAME as "departamento",
count(*) as "totalEmpleados"
from DEPARTMENTS d, EMPLOYEES e
Where e.DEPARTMENT_ID = d.DEPARTMENT_ID
Group by DEPARTMENT_NAME
Seleccionar el nombre de los departamentos donde trabaja el empleado.
select DEPARTMENT_NAME ,count(EMPLOYEE_ID)
from DEPARTMENTS d, EMPLOYEES e
Where e.DEPARTMENT_ID = d.DEPARTMENT_ID
Group by DEPARTMENT_NAME
Order by DEPARTMENT_NAME
---------------------------------------------
Cursor
Declare
cursor miCursor is
select department_id ,
count(*)
From employees
group by department_id
Order by department_id;
vDepartamento employeeS.employee_id%type;
vTotEmpleado integer;
Begin
open miCursor;
dbms_output.put_line('Departamento TotEmpleado');
loop
fetch miCursor into vDepartamento, vTotEmpleado;
exit when miCursor%notfound;
dbms_output.put_line(vDepartamento || ' ' || vTotEmpleado);
end loop;
close miCursor;
End;
----------------------------------------------
Saber cual es el nombre de un codigo de un depto.
Declare
vNumeroDepto integer := 10;
vNombreDepto char(50);
begin
Select DEPARTMENT_NAME into vNombreDepto
from departments
where department_id = vNumeroDepto;
dbms_output.put_line('El codigo del depto es : ' || vNumeroDepto);
dbms_output.put_line('El codigo del depto es : ' || vNombreDepto);
end;
Suscribirse a:
Entradas (Atom)