Sei sulla pagina 1di 2

delimiter // create procedure valida(in fecha_compra date, out fc_cont date, out fc_cred date , out fc_res date)

begin select fecha_compra into fc_cont from contado where contado.fecha_compra=fecha_c ompra; select fecha_compra into fc_cred from credito where credito.fecha_compra=fecha_c ompra; select fecha_compra into fc_res from resumen where resumen.fecha_compra=fecha_co mpra; end; // delimiter ; delimiter // create procedure icontado(in fecha_compra date, in estatus_compra varchar(20), o ut 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), o ut tfcontado int) begin select count(nf) into tfcontado from compras where fecha_compra=compras.fecha_co mpra and compras.estatus_compra='contado'; end; // delimiter ; delimiter // create procedure icredito(in fecha_compra date, in estatus_Compra varchar(20), o ut scredito int) begin select 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), o ut tfcredito int) begin select count(nf) into tfcredito from compras where fecha_compra=compras.fecha_co mpra and compras.estatus_compra='credito'; end; // delimiter ; delimiter // create trigger examen after insert on compras for each row begin declare temporal1 int; declare temporal2 int;

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) then insert into contado values(new.fecha_compra,@scontado,@tfcontado); elseif(temporal1>0 and @fc_cont is null) then insert into contado values(new.fecha_compra,@scontado,@tfcontado); elseif(temporal1>0 and @fc_cont is not null and new.fecha_compra=@fc_cont) then update contado set importe_compra=@scontado, total_facturas=@tfcontado where con tado.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) then insert into credito values(new.fecha_compra,@scredito,@tfcredito); elseif(temporal2>0 and @fc_cred is null) then insert into credito values(new.fecha_compra,@scredito,@tfcredito); elseif(temporal2>0 and @fc_cred is not null and new.fecha_compra=@fc_cred) then update credito set importe_compra=@scredito, total_facturas=@tfcredito where cre dito.fecha_compra=@fc_cred; end if; if(temporal3=0) then insert into resumen values(new.fecha_compra,@scontado,@scredito); elseif(temporal3>0 and @fc_res is null) then insert into resumen values(new.fecha_compra,@scontado,@scredito); elseif(temporal3>0 and @fc_res is not null and new.fecha_compra=@fc_res) then update resumen set importe_compra_contado=@scontado, importe_compra_credito=@scr edito where resumen.fecha_compra=@fc_res; end if; end; // delimiter ;

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

Potrebbero piacerti anche