i am trying to crete a procedure to my DDBB and it is reporting me an error. This is the code:
CREATE PROCEDURE insertarFactura(in idPedidoVar int)
BEGIN
#la variable @cuantos para saber si la tabla esta vacia o no
#var @cuantos tells me if table Facturas is empty
select @cuantas:= COUNT(idFactura) from Facturas;
#si la tabla esta vacía seteo el id a 1, sino cojo el maximo del id y le sumo 1
#if Facturas is empty i set @id to 1 else i take the next id from the table
IF @cuantas>0 THEN
select @id:= SELECT max(idFactura)+1 from Facturas;
ELSE
SET @id=1;
END IF;
#calculo el importe del pedido ¿la tabla pedidos en esta relación es necesaria?
#next, i calculate the price of the order
SELECT @importe:= sum(ProductosPedidos.Cantidad*Productos.Precio) from ProductosPedidos INNER JOIN Productos on ProductosPedidos.idProducto=Productos.idProducto where idPedido=idPedidoVar;
#inserto en la tabla Facturas la factura correspondiente
#insert on the table Facturas the bill of the order
INSERT INTO Facturas (idFactura,idPedido,Importe) values (@id,idPedidoVar,@importe);
END
And that is the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
I think this is probably a delimiter error.
Try this
delimiter //
CREATE PROCEDURE insertarFactura(in idPedidoVar int)
BEGIN
#la variable @cuantos para saber si la tabla esta vacia o no
#var @cuantos tells me if table Facturas is empty
select @cuantas:= COUNT(idFactura) from Facturas;
#si la tabla esta vacía seteo el id a 1, sino cojo el maximo del id y le sumo 1
#if Facturas is empty i set @id to 1 else i take the next id from the table
IF @cuantas>0 THEN
select @id:= (SELECT max(idFactura)+1 from Facturas);
ELSE
SET @id=1;
END IF;
#calculo el importe del pedido ¿la tabla pedidos en esta relación es necesaria?
#next, i calculate the price of the order
SELECT @importe:= sum(ProductosPedidos.Cantidad*Productos.Precio) from ProductosPedidos INNER JOIN Productos on ProductosPedidos.idProducto=Productos.idProducto where idPedido=idPedidoVar;
#inserto en la tabla Facturas la factura correspondiente
#insert on the table Facturas the bill of the order
INSERT INTO Facturas (idFactura,idPedido,Importe) values (@id,idPedidoVar,@importe);
END//
delimiter ;