我正在尝试为我的 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 ;
关于mysql - mysql 过程出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35580262/