viernes, 24 de mayo de 2013

Script aplicando transacciones


Les dejo el codigo de la clase del dia 23 de mayo de 2013

delimiter //
create procedure cargarventa(in cprod varchar(8), in cantidad int) begin
declare registros int;
declare nprod varchar(200);
declare ncosto decimal(10,2);
declare ncantidad int;
  set registros = (select count(*) from producto where codprod = cprod); 
   if registros > 0 then  
       start transaction;   
       insert into venta values (null,cprod,cantidad);   
       select * from venta;   
       select * from control;   
       set nprod = (select nomprod from producto where codprod = cprod);   
       set ncosto = (select costo from producto where codprod = cprod);
       set ncantidad = (select existencias from producto where codprod = cprod);
         if ncantidad > cantidad then
            select 'se vende el producto y se actualizan existencias';
            update producto set existencias = ncantidad - cantidad where codprod = cprod;
            select nprod;
            commit;
        else
           select 'producto no vendido.....';
           rollback;
        end if;
     select * from venta;
     select * from control;
     select * from producto;
   else
    select 'No existe el producto....';
  end if;
end//
delimiter ;

No hay comentarios:

Publicar un comentario