gpt4 book ai didi

mysql - 在mysql中创建存储过程

转载 作者:行者123 更新时间:2023-11-29 19:10:33 27 4
gpt4 key购买 nike

这段代码有什么问题

CREATE DEFINER=`root`@`localhost` PROCEDURE `diff`()
BEGIN
declare max_date datetime;
declare tday_date datetime;
declare main_id varchar(20);

select concat('#GNC', '3') into main_id; #error shows here


declare m1 int;
declare m2 int;
select max(order_date) into max_date from `order`;
#select max_date;
select curdate() into tday_date;
select extract(month from max_date) into m1;
select extract(month from tday_date) into m2;

if m1 != m2 then
TRUNCATE `grocery`.`order_seqid`;
end if;



select m1, m2, main_id;
END

它给出了像缺少结尾这样的错误。如果我删除 if 语句,它就可以正常工作。

最佳答案

你就快到了:)我看到的唯一问题是你的所有声明都不是在你的逻辑的开头并且你没有使用@提到的分隔符阿尔瓦罗;试试这个:

DELIMITER $$

CREATE PROCEDURE `diff`()
BEGIN
declare max_date datetime;
declare tday_date datetime;
declare main_id varchar(20);
declare m1 INT;
declare m2 int;

select concat('#GNC', '3') into main_id;

select max(order_date) into max_date from `order`;
#select max_date;
select curdate() into tday_date;
select extract(month from max_date) into m1;
select extract(month from tday_date) into m2;

if m1 != m2 then
TRUNCATE `grocery`.`order_seqid`;
end if;



select m1, m2, main_id;
END $$

关于mysql - 在mysql中创建存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43116963/

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