English version (when available)
-- las siguientes instrucciones crean las tablas con las estructuras de hospital y todos sus datos: CREATE TABLE yXXX_paciente AS SELECT * from hospital.paciente; CREATE TABLE yXXX_trabajador AS SELECT * from hospital.trabajador; CREATE TABLE yXXX_medico AS SELECT * from hospital.medico; CREATE TABLE yXXX_enfermero AS SELECT * from hospital.enfermero; CREATE TABLE yXXX_historial AS SELECT * from hospital.historial; CREATE TABLE yXXX_especialidad AS SELECT * from hospital.especialidad; CREATE TABLE yXXX_linea_historial AS SELECT * from hospital.linea_historial; CREATE TABLE yXXX_tiene_un AS SELECT * from hospital.tiene_un; CREATE TABLE yXXX_auxiliar_m (colegiado varchar(9), primary key (colegiado)); CREATE TABLE yXXX_auxiliar_h (colegiado varchar(9), primary key (colegiado));Solución alternativa:-- las siguientes instrucciones crean las tablas con las estructuras de hospital pero sin datos: CREATE TABLE yXXX_paciente LIKE hospital.paciente; CREATE TABLE yXXX_trabajador LIKE hospital.trabajador; CREATE TABLE yXXX_medico LIKE hospital.medico; CREATE TABLE yXXX_enfermero LIKE hospital.enfermero; CREATE TABLE yXXX_historial LIKE hospital.historial; CREATE TABLE yXXX_especialidad LIKE hospital.especialidad; CREATE TABLE yXXX_linea_historial LIKE hospital.linea_historial; CREATE TABLE yXXX_tiene_un LIKE hospital.tiene_un; CREATE TABLE yXXX_auxiliar_m (colegiado varchar(9), primary key (colegiado)); CREATE TABLE yXXX_auxiliar_h (colegiado varchar(9), primary key (colegiado));
delimiter // create procedure yXXX_alta_especialidad (in idesp int(10), in tipesp varchar(50)) begin insert into yXXX_especialidad (id, tipo) values (idesp, tipesp); end; // delimiter ; call yXXX_alta_especialidad(8, 'nuevapal'); select * from yXXX_especialidad; -- para comprobar que se ha insertado.
delimiter // create procedure yXXX_borra_especialidad (in idesp int(10)) begin delete from yXXX_especialidad where id=idesp; end; // delimiter ; call yXXX_borra_especialidad(8); select * from yXXX_especialidad; -- para comprobar que se ha borrado.
delimiter // create procedure yXXX_borra_todo_especialidad () begin delete from yXXX_especialidad; end; // delimiter ; call yXXX_borra_todo_especialidad(); select * from yXXX_especialidad; -- para comprobar que se ha borrado.
delimiter // create procedure yXXX_desactiva_paciente (in sippac varchar(7)) begin declare prov varchar(50); declare act tinyint(1); select provincia, activo into prov,act from yXXX_paciente where sip=sippac; if (prov <> ‘ALICANTE’) && (prov <> ‘VALENCIA’ ) && (prov <>’CASTELLON’) then update yXXX_paciente set activo=0 where sip=sippac; end if; end; // delimiter ; select sip, activo, provincia from yXXX_paciente where sip = '1000007'; --paciente de Huelva activo call yXXX_desactiva_paciente('1000007'); select sip, activo, provincia from yXXX_paciente where sip = '1000007'; --paciente de Huelva desactivado
delimiter // create procedure yXXX_desactiva_paciente2 (in sippac varchar(7)) begin declare his int(11); declare anyo int(5); select idhistorial into his from yXXX_tiene_un where idpaciente=sippac; select date_format(now(),'%Y')-date_format(max(fecha),’%Y’) into anyo from yXXX_linea_historial where idhistorial=his; if anyo >=1 then update yXXX_paciente set activo = 0 where sip=sippac; end if; end; // delimiter ; select activo from yXXX_paciente where sip='1000035'; -- paciente activo select idhistorial from yXXX_tiene_un where idpaciente='1000035'; -- vemos historial select max(fecha) from yXXX_linea_historial where idhistorial= 718153; -- vemos fecha call yXXX_desactiva_paciente2('1000035'); select activo from yXXX_paciente where sip='1000035'; -- paciente desactivado
delimiter // create procedure yXXX_alta_auxiliar () begin declare done bool default 0; declare col varchar(9); declare sex varchar(1); declare aux cursor for select colegiado,sexo from yXXX_trabajador where colegiado not in (select colegiado from medico) and colegiado not in (select colegiado from enfermero); declare continue handler for sqlstate ‘02000’ set done=1; open aux; repeat fetch aux into col,sex; if not done then if sex=’M’ then insert into yXXX_auxiliar_m (colegiado) values (col); else insert into yXXX_auxiliar_h (colegiado) values (col); end if; end if; until done end repeat; close aux; end; // delimiter ; select count(*) from yXXX_auxiliar_m; -- inicialmente tiene 0 filas select count(*) from yXXX_auxiliar_h; -- inicialmente tiene 0 filas select count(*),sexo from yXXX_trabajador where colegiado not in (select colegiado from medico) and colegiado not in (select colegiado from enfermero); -- insertar 239 en h y 241 en m call yXXX_alta_auxiliar(); select count(*) from yXXX_auxiliar_m -- 241 filas select count(*) from yXXX_auxiliar_h -- 239 filas
Las funciones han de devolver un valor obligatoriamente. Falta, por tanto, el RETURN. CREATE FUNCTION total_activos() RETURNS int DETERMINISTIC BEGIN DECLARE total int; SELECT count(*) INTO total FROM yXXX_trabajador; RETURN total; END;
Calcula el total de créditos de teoría y de práctica que imparte un profesor dado, los suma y el valor resultante lo devuelve como salida de la función.
Después de cualquier operación de inserción de datos en la tabla medico.
Antes de cualquier borrado que se realice sobre la tabla medico.
DELIMITER // CREATE PROCEDURE firma_medico (IN p_medico VARCHAR(9)) BEGIN declare total int; SELECT COUNT INTO total FROM linea_historial WHERE idMedico=p_medico; IF total>0 then UPDATE medico SET firmado = 'S' WHERE colegiado=p_medico; ELSE UPDATE medico SET firmado = 'N' WHERE colegiado=p_medico; END if; END; //