Miexamen de Triggers!!!

  • Upload
    bruja91

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 8/3/2019 Miexamen de Triggers!!!

    1/2

    delimiter //create procedure valida(in fecha_compra date, out fc_cont date, out fc_cred date, out fc_res date)beginselect fecha_compra into fc_cont from contado where contado.fecha_compra=fecha_compra;

    select fecha_compra into fc_cred from credito where credito.fecha_compra=fecha_compra;select fecha_compra into fc_res from resumen where resumen.fecha_compra=fecha_compra;end;//delimiter ;

    delimiter //create procedure icontado(in fecha_compra date, in estatus_compra varchar(20), out scontado int)begin

    select sum(importe_compra) into scontado from compras where fecha_compra=compras.fecha_compra and compras.estatus_compra='contado';end;//delimiter ;

    delimiter //create procedure fcontado(in fecha_compra date, in estatus_compra varchar(20), out tfcontado int)beginselect count(nf) into tfcontado from compras where fecha_compra=compras.fecha_compra and compras.estatus_compra='contado';end;

    //delimiter ;

    delimiter //create procedure icredito(in fecha_compra date, in estatus_Compra varchar(20), out scredito int)beginselect sum(importe_compra) into scredito from compras where fecha_Compra=compras.fecha_compra and compras.estatus_compra='credito';end;//delimiter ;

    delimiter //create procedure fcredito(in fecha_compra date, in estatus_compra varchar(20), out tfcredito int)beginselect count(nf) into tfcredito from compras where fecha_compra=compras.fecha_compra and compras.estatus_compra='credito';end;//delimiter ;

    delimiter //create trigger examen after insert on compras

    for each row begindeclare temporal1 int;declare temporal2 int;

  • 8/3/2019 Miexamen de Triggers!!!

    2/2

    declare temporal3 int;select count(*) into temporal1 from contado;select count(*) into temporal2 from credito;select count(*) into temporal3 from resumen;call valida(new.fecha_compra,@fc_cont,@fc_cred,@fc_res);call icontado(new.fecha_compra,new.estatus_Compra,@scontado);call fcontado(new.fecha_compra,new.estatus_Compra,@tfcontado);

    if(temporal1=0) theninsert into contado values(new.fecha_compra,@scontado,@tfcontado);elseif(temporal1>0 and @fc_cont is null) theninsert into contado values(new.fecha_compra,@scontado,@tfcontado);elseif(temporal1>0 and @fc_cont is not null and new.fecha_compra=@fc_cont) thenupdate contado set importe_compra=@scontado, total_facturas=@tfcontado where contado.fecha_compra=@fc_cont;end if;call icredito(new.fecha_compra,new.estatus_Compra,@scredito);call fcredito(new.fecha_compra,new.estatus_Compra,@tfcredito);if(temporal2=0) theninsert into credito values(new.fecha_compra,@scredito,@tfcredito);

    elseif(temporal2>0 and @fc_cred is null) theninsert into credito values(new.fecha_compra,@scredito,@tfcredito);elseif(temporal2>0 and @fc_cred is not null and new.fecha_compra=@fc_cred) thenupdate credito set importe_compra=@scredito, total_facturas=@tfcredito where credito.fecha_compra=@fc_cred;end if;if(temporal3=0) theninsert into resumen values(new.fecha_compra,@scontado,@scredito);elseif(temporal3>0 and @fc_res is null) theninsert into resumen values(new.fecha_compra,@scontado,@scredito);elseif(temporal3>0 and @fc_res is not null and new.fecha_compra=@fc_res) thenupdate resumen set importe_compra_contado=@scontado, importe_compra_credito=@scredito where resumen.fecha_compra=@fc_res;

    end if;end;//delimiter ;

    insert into compras values(1,'2010-06-09',1000,'contado');