gpt4 book ai didi

mysql - mysql 过程出错

转载 作者:行者123 更新时间:2023-11-29 21:24:20 25 4
gpt4 key购买 nike

我正在尝试为我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com