Fernando Fernández López
Base de Datos
1ºDAM
Realiza los siguientes ejercicios.
Sobre la BD tema 6 y obligatorio calcular el dato solicitado utilizando un cursor.
1.- Realiza un procedimiento que nos diga cuantos empleados de la tabla empleados de la base
de datos tema3 tienen como oficio "VENDEDOR".
1.1
DELIMITER //
CREATE PROCEDURE ejercicio1_1()
BEGIN
DECLARE nfila INT;
DECLARE lrf BOOL;
DECLARE tmp INT;
DECLARE cursorvende CURSOR FOR SELECT emple.EMP_NO FROM emple WHERE
oficio="vendedor";
DECLARE CONTINUE handler FOR NOT FOUND SET lrf=1;
SET lrf=0;
OPEN cursorvende;
SET nfila=0;
L_cursor: LOOP
fetch cursorvende INTO tmp;
if lrf=1 then
leave l_cursor;
END if;
SET nfila=nfila+1;
END loop l_cursor;
close cursorvende;
SELECT nfila;
END; //
DELIMITER;
1.2
DELIMITER //
CREATE PROCEDURE ejercicio1_2()
BEGIN
DECLARE nfila INT;
DECLARE lrf BOOL;
DECLARE tmp INT;
DECLARE cursorvende CURSOR FOR SELECT emp_no FROM emple WHERE
oficio="vendedor";
DECLARE CONTINUE HANDLER FOR NOT FOUND SET lrf=1;
SET lrf=0;
OPEN cursorvende;
SET nfila=0;
while lrf=0 do
FETCH cursorvende INTO tmp;
if lrf=0 then
SET nfila=nfila+1;
end if;
END while;
CLOSE cursorvende;
SELECT nfila;
END; //
DELIMITER;
1
Fernando Fernández López
Base de Datos
1ºDAM
1.3
Delimiter //
Create procedure ejercicio1p3()
begin
declare nfila int;
declare lrf bool;
declare tmp int;
declare cursorvende Cursor for select EMP_NO from EMPLE where oficio
="vendedor";
declare continue handler for not found set lrf =1;
set lrf = 0;
open cursorvende;
set nfila = 0;
repeat
fetch cursorvende into tmp;
if lrf=0 then
SET nfila = nfila+1;
end if;
until lrf=1
end repeat;
close cursorvende;
select nfila;
END; //
delimiter ;
2.- Realiza una función que me devuelva la suma de todos los salarios de los empleados.
2.1
Delimiter //
Create function ejercicio2() RETURNS int
begin
declare suma int;
declare lrf bool;
declare tmp int;
declare cursorsalario Cursor for select salario from EMPLE;
declare continue handler for not found set lrf =1;
set lrf = 0;
open cursorsalario;
set suma = 0;
L_cursor: LOOP
fetch cursorsalario into tmp;
if lrf=0 then SET suma = suma+tmp;
End if;
if lrf=1 then leave l_cursor;
end if;
END loop L_cursor;
close cursorsalario;
return suma;
END
/
delimiter ;
2.2
Delimiter //
Create function ejercicio() RETURNS int
begin
2
Fernando Fernández López
Base de Datos
1ºDAM
declare suma int;
declare lrf bool;
declare tmp int;
declare cursorsalario Cursor for select emple.salario from EMPLE;
declare continue handler for not found set lrf =1;
set lrf = 0;
open cursorsalario;
set suma = 0;
L_cursor: repeat
fetch cursorsalario into tmp;
if lrf=0 then
SET suma=suma+tmp;
end if;
until lrf=1
end repeat;
close cursorsalario;
return suma;
END
/
delimiter ;
2.3
Delimiter //
Create function ejercicio2_3() RETURNS int
begin
declare suma int;
declare lrf bool;
declare tmp int;
declare cursorsalario Cursor for select emple.salario from EMPLE;
declare continue handler for not found set lrf =1;
set lrf = 0;
open cursorsalario;
set suma = 0;
L_cursor: while lrf=0 do
FETCH cursorsalario INTO tmp;
if lrf=0 then
SET suma=suma+tmp;
end if;
END while;
close cursorsalario;
return suma;
END
/
delimiter ;
3.- Modifica el ejercicio anterior para que sume los salarios de los empleados que sean de
oficio "VENDEDOR".
Delimiter //
Create function ejercicio() RETURNS int
begin
declare suma int;
declare lrf bool;
declare tmp int;
3
Fernando Fernández López
Base de Datos
1ºDAM
declare cursorsalario Cursor for select emple.salario from EMPLE WHERE
emple.OFICIO="VENDEDOR";
declare continue handler for not found set lrf =1;
set lrf = 0;
open cursorsalario;
set suma = 0;
L_cursor: LOOP
fetch cursorsalario into tmp;
if lrf=0 then SET suma = suma+tmp;
End if;
if lrf=1 then leave l_cursor;
end if;
END loop L_cursor;
close cursorsalario;
return suma;
END
/
delimiter ;
3.2
Delimiter //
Create function ejercicio() RETURNS int
begin
declare suma int;
declare lrf bool;
declare tmp int;
declare cursorsalario Cursor for select emple.salario from EMPLE WHERE
emple.OFICIO="VENDEDOR";
declare continue handler for not found set lrf =1;
set lrf = 0;
open cursorsalario;
set suma = 0;
L_cursor: repeat
fetch cursorsalario into tmp;
if lrf=0 then
SET suma=suma+tmp;
end if;
until lrf=1
end repeat;
close cursorsalario;
return suma;
END
/
delimiter ;
3.3
Delimiter //
Create function ejercicio2_3() RETURNS int
begin
declare suma int;
declare lrf bool;
declare tmp int;
declare cursorsalario Cursor for select emple.salario from EMPLE WHERE
emple.OFICIO="VENDEDOR";
4
Fernando Fernández López
Base de Datos
1ºDAM
declare continue handler for not found set lrf =1;
set lrf = 0;
open cursorsalario;
set suma = 0;
L_cursor: while lrf=0 do
FETCH cursorsalario INTO tmp;
if lrf=0 then
SET suma=suma+tmp;
end if;
END while;
close cursorsalario;
return suma;
END
/
delimiter ;