【问题标题】:What is wrong with my mysql stored procedure?我的mysql存储过程有什么问题?
【发布时间】:2019-09-02 07:02:49
【问题描述】:

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

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

【问题讨论】:

    标签: mysql if-statement stored-procedures


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多