我正在尝试在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
(INdeviceIdentifier
VARCHAR(45),INdeviceName
VARCHAR(45),INdeviceLocation
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')中简化语法
答案 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 ;