gpt4 book ai didi

MySQL - 存储过程 - 仅使用 while 循环获取最后一条记录

转载 作者:行者123 更新时间:2023-11-29 19:52:49 24 4
gpt4 key购买 nike

我试图继续添加表中的所有价格值。我努力了n_total_price = n_total_price + d_price;

这是一项作业,我必须使用存储过程

它只是给我最后一条记录

    DELIMITER $$
CREATE PROCEDURE P_TOTAL_PRICES(OUT N_TOTAL_PRICE DECIMAL(30,2))
BEGIN
DECLARE D_PRICE DECIMAL(30,2);
DECLARE ABC DECIMAL(30,2);
DECLARE FOUND BOOLEAN;
DECLARE CURSOR_1 CURSOR FOR SELECT PRICE FROM PRODUCT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET FOUND = FALSE;
SET FOUND = TRUE;
OPEN CURSOR_1;
FETCH CURSOR_1 INTO ABC;
WHILE FOUND DO
SET N_TOTAL_PRICE = N_TOTAL_PRICE + D_PRICE;
FETCH CURSOR_1 INTO ABC;
END WHILE;
CLOSE CURSOR_1;
END; $$

最佳答案

DELIMITER $$
CREATE PROCEDURE P_TOTAL_PRICES(OUT N_TOTAL_PRICE DECIMAL(30,2))
BEGIN
DECLARE D_PRICE DECIMAL(30,2);
DECLARE ABC DECIMAL(30,2);
DECLARE FOUND BOOLEAN;
DECLARE CURSOR_1 CURSOR FOR SELECT PRICE FROM PRODUCT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET FOUND = FALSE;
SET FOUND = TRUE;
OPEN CURSOR_1;
FETCH CURSOR_1 INTO ABC;
**SET N_TOTAL_PRICE = 0;**
WHILE FOUND DO SET N_TOTAL_PRICE = N_TOTAL_PRICE +D_PRICE;
FETCH CURSOR_1 INTO ABC;
END WHILE;
CLOSE CURSOR_1;
END; $$

关于MySQL - 存储过程 - 仅使用 while 循环获取最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40775813/

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