在MySQL / MariaDB中创建过程并定义变量会导致错误#1064

时间:2019-05-04 15:28:18

标签: mysql stored-procedures mariadb

我正在尝试在mysql / mariadb中创建一个带有声明变量的SP-但我看不到,这是怎么回事!?!

DROP PROCEDURE IF EXISTS UpdateReceiverDevice;
DELIMITER $$

CREATE PROCEDURE `UpdateReceiverDevice`(IN `deviceIdentifier` VARCHAR(45), IN `deviceName` VARCHAR(45), IN `deviceLocation` VARCHAR(45), IN `informations` TEXT) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER
BEGIN
    DECLARE receiverDeviceId AS INT(11) DEFAULT 0
    SET receiverDeviceId = (SELECT ID FROM ReceiverDevice WHERE DeviceIdentifier = deviceIdentifier);
    IF (receiverDeviceId > 0) BEGIN
        UPDATE ReceiverDevice SET Informations = informations WHERE ID = receiverDeviceId;
    ELSE
        INSERT INTO ReceiverDevice (DeviceName, DeviceLocation, DeviceIdentifier, Informations) VALUES(deviceName, deviceLocation, deviceIdentifier, informations);
    END IF
END $$

DELIMITER ;
  

MySQL返回此错误:

     

Eh oder mehrere Fehler sind aufgetretenwährendIhre Anfrage verarbeitet wurde:   Df folgende Abfrage ist fehlgeschlagen:“创建程序UpdateReceiverDevice(IN deviceIdentifier VARCHAR(45),IN deviceName VARCHAR(45),IN deviceLocation VARCHAR(45),IN { {1}}文本)不确定的修改SQL数据SQL安全性定义接受者deviceID AS INT(11)SET ReceiverDeviceId =(从ReceiverDevice的选择ID WHERE DeviceIdentifier = deviceIdentifier)IF receiverDeviceId> 0 UPDATE ReceiverDevice SET信息=信息WHERE ID = @receiverDeviceId ELSE插入接收器设备(设备名称,设备位置,设备标识符,信息)值(设备名称,设备位置,设备标识符,信息)END IF“

     

MySQL融合:#1064-der SQL语法中的Fehler。在'DECLARE接收器设备ID AS INT(11)SET接收器设备ID =(Zeile 1中的SELECT ID FROM Re')中简化语法

这是我的服务器:

  • 服务器类型:MariaDB
  • 服务器版本:10.3.11-MariaDB-1:10.3.11 + maria〜bionic-mariadb.org二进制分发版
  • Protokoll版本:10
  • Server-Zeichensatz:UTF-8 Unicode(utf8)

1 个答案:

答案 0 :(得分:0)

注释“每个语句都需要终止”中的提示非常有帮助。

DROP PROCEDURE IF EXISTS UpdateReceiverDevice;
DELIMITER $$

CREATE PROCEDURE `UpdateReceiverDevice`(IN `deviceIdentifier` VARCHAR(45), IN `deviceName` VARCHAR(45), IN `deviceLocation` VARCHAR(45), IN `informations` TEXT) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER 
BEGIN
    DECLARE receiverDeviceId INT(11) DEFAULT 0;
    SELECT ID INTO receiverDeviceId FROM ReceiverDevice WHERE DeviceIdentifier = deviceIdentifier;

    IF receiverDeviceId > 0 THEN
        UPDATE ReceiverDevice SET Informations = informations WHERE ID = receiverDeviceId;
    ELSE
        INSERT INTO ReceiverDevice (DeviceName, DeviceLocation, DeviceIdentifier, Informations) VALUES(deviceName, deviceLocation, deviceIdentifier, informations);
    END IF;
END $$

DELIMITER ;