mysql - mysql 过程出错

标签 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 ;

关于mysql - mysql 过程出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35580262/

相关文章:

laravel 5.3中mysql和mongodb的关系

mysql - 子查询不起作用,但替换子查询结果可以

java - 由 : java. sql.SQLException : ORA-06550: line 1, 第 7 列引起:PLS-00306:调用 'PR_SP_FAHMI' 时参数数量或类型错误

php - 搜索数据库以匹配用户输入时出现问题

php - 将数据从一个表插入到另一个表几乎可以正常工作

带参数语法的 MySQL 过程

java - 挣扎着用spring SimpleJdbcCall调用Oracle函数

mysql - 在 MySQL 5.0 中创建 MySQL 空间过程

database - 对某些值进行特殊处理的 PL/SQL 过程 : UPDATE uppercase names to initcaps,

php - 为什么 MySQL 语句被忽略?