Create a function that modifies the sale price of a product based on the quantity in stock: a) If the amount is 300, the price increase will be of a value of 5 * /
I am trying to do this exercise and it does not work out in any way. I leave as I've been thinking, although the code that I attached gives you syntax errors.
drop function if exists funcion2;
set global log_bin_trust_function_creators = 1 ;
DELIMITER //
create function funcion2(cod_producto int(5))
returns int(5)
begin
declare PrecioVenta int(5);
select PrecioVenta
from productos
where CodigoProducto.productos=cod_producto;
if PrecioVenta<100 then update productos set PrecioVenta=PrecioVenta+15;
elseif (PrecioVenta>100 and PrecioVenta<300) then update productos set PrecioVenta=PrecioVenta+10;
else update productos set PrecioVenta=PrecioVenta+5;
end//
DELIMITER ;
The product table and a new attempt with what you have recommended me, but nothing ...
drop function if exists funcion2;
set global log_bin_trust_function_creators = 1 ;
DELIMITER //
create function funcion2(cod_producto int(5))
returns int(5)
begin
declare PrecioVenta int(5);
select CantidadEnStock, PrecioVenta
from productos
where productos.CodigoProducto=cod_producto;
UPDATE productos
SET PrecioVenta = CASE
WHEN CantidadEnStock < 100 THEN PrecioVenta + 15
WHEN CantidadEnStock >= 100 AND CantidadEnStock <= 300 THEN PrecioVenta + 10
WHEN CantidadEnStock > 300 THEN PrecioVenta + 5
END
WHERE productos.CodigoProducto = cod_producto;
return PrecioVenta;
end//
DELIMITER ;