gpt4 book ai didi

mysql - 我的 mysql 存储过程出了什么问题?

转载 作者:行者123 更新时间:2023-11-29 06:28:47 25 4
gpt4 key购买 nike

我有一个存储过程,用于检查表中是否已存在新条目。如果存在,则不会发生插入。当我运行它时,出现错误

DROP PROCEDURE IF EXISTS AddPriority2;
DELIMITER $$

CREATE PROCEDURE AddPriority2
(
IN strName VARCHAR(100),
OUT itExists INT
)
BEGIN
DECLARE
SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId = 1;

IF(itExists = 0) THEN
INSERT INTO priorities
(
NAME,
StatId
)
VALUES
(
strName,
1
);
END IF;
END

这是错误

Query: CREATE PROCEDURE AddPriority2 ( IN strName VARCHAR(100), OUT itExists INT ) BEGIN DECLARE SELECT COUNT(Id) INTO itExists FROM pr...

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId =' at line 8

最佳答案

1)您不能声明 select 语句 - 声明必须用于变量..(并且我不会为此使用输出参数)2)或者您可以使用exists代替

if not exists (select 1 from priorities WHERE Name = strName AND StatId = 1) then
insert...
end if;

关于mysql - 我的 mysql 存储过程出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57752643/

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