Ver Mensaje Individual
  #12  
Antiguo 16-07-2007
capo979 capo979 is offline
Miembro
 
Registrado: abr 2007
Posts: 87
Reputación: 18
capo979 Va por buen camino
este es mi SP

CREATE PROCEDURE GEN_ANIMALES (
ESTABLECIMIENTO INTEGER,
ACTUAL DATE,
CON_EVENTO VARCHAR (1) CHARACTER SET NONE)
RETURNS (
ANIMAL INTEGER,
RP VARCHAR (10) CHARACTER SET NONE,
SEXO INTEGER,
RAZA INTEGER,
RAZA_NOMBRE VARCHAR (50) CHARACTER SET NONE,
RODEO INTEGER,
CATEGORIA INTEGER,
CATEGORIA_NOMBRE VARCHAR (50) CHARACTER SET NONE,
CRONO_DENT INTEGER,
ESTADO_REPRO INTEGER,
NACIMIENTO DATE,
EDAD INTEGER,
POTRERO INTEGER,
ACTIVO VARCHAR (1) CHARACTER SET NONE,
COND_CORP INTEGER,
GDR INTEGER,
PESO INTEGER,
RECHAZADO VARCHAR (1) CHARACTER SET NONE,
FRAME FLOAT,
ESTADO_ACTUAL VARCHAR (50) CHARACTER SET NONE,
ULTIMO_EVENTO VARCHAR (255) CHARACTER SET NONE,
EDADCAPTION VARCHAR (12) CHARACTER SET NONE,
ESTADO_LACTACION INTEGER)
AS -- estado_actual, ultimo_evento, datos_eve se deben calcular
declare variable mensaje varchar(50);
declare variable ultimo_eve integer;
declare variable evento varchar(255);

BEGIN
for select ta.id_animal, ta.id_rp, cs.tipo, ta.raza, cr.nombre, ta.rodeo, ta.categoria, cc.nombre,
ta.cronologia_dentaria, ta.estado_reproductivo, ta.fecha_nacimiento, ta.potrero,
ta.activo, ta.condicion_corporal, ta.gdr, ta.peso, ta.rechazado, ta.frame, ta.estado_lactacion
from tab_animales ta, cod_sexos cs, cod_categorias cc, cod_razas cr
where (ta.establecimiento = :establecimiento) and (ta.sexo = cs.id_sexo)
and (ta.raza = cr.id_raza) and (ta.categoria = cc.id_categoria)
into :animal, :rp, :sexo, :raza, :raza_nombre, :rodeo, :categoria, :categoria_nombre, :crono_dent,
:estado_repro, :nacimiento, otrero, :activo, :cond_corp, :GDR, eso, :rechazado, :frame, :ESTADO_LACTACION
do
begin
if (eso is null) then
begin
select first 1 ep.peso from eve_eventos ee, eve_peso ep
where ee.id_evento = ep.id_evento
and ee.animal = :animal
and ee.establecimiento = :establecimiento
order by ee.fecha desc
into eso;
end

edad = (actual - nacimiento)/365;
if (edad < 2) then
EdadCaption = cast(((actual - nacimiento)/30) as varchar(6))||' meses';
else
EdadCaption = cast(edad as varchar(6))||' años';

-- recupero el estado actual desde el sp genereado para calcular el estado actual
select estado_actual from gen_estado_actual(:animal)
into :estado_actual;

--me fijo si tengo que generar_datos del evento
if (con_evento = 'S') then
begin
select max(id_evento) from eve_eventos where (establecimiento = :establecimiento) and (animal = :animal)
into :ultimo_eve;

select nombretipoevento, infoevento from get_datos_eventos_grupal(:ultimo_eve, null, :establecimiento)
into :evento, :ultimo_evento;

ultimo_evento = evento || ', '||ultimo_evento;
end
SUSPEND;
end
END

si yo comento esto

if (eso is null) then
begin
select first 1 ep.peso from eve_eventos ee, eve_peso ep
where ee.id_evento = ep.id_evento
and ee.animal = :animal
and ee.establecimiento = :establecimiento
order by ee.fecha desc
into eso;
end

y esto

select max(id_evento) from eve_eventos where (establecimiento = :establecimiento) and (animal = :animal)
into :ultimo_eve;

anda rápido pero si lo dejo se pone muy lento

tengo todos los indices creados

y utilizo firebird 1.5
Responder Con Cita