mysql 程序出错

Error on mysql procedure

我正在尝试为我的 DDBB 创建一个程序,但它向我报告错误。这是代码:

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

这就是错误:

1064 - 你的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1

行的 '' 附近使用的正确语法

我认为这可能是分隔符错误。

试试这个

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 ;