Mysql 5.7 存储过程报错

标签 mysql stored-procedures stored-functions java-stored-procedures

我尝试创建以下格式的存储过程: 分隔符 $$

CREATE PROCEDURE `CreateInsertLocation` (tableName VARCHAR(255),ts BIGINT(20),systs INT(20),lat FLOAT,lon FLOAT)
BEGIN
    DECLARE FoundCount INT;

    SELECT COUNT(1) INTO FoundCount
    FROM information_schema.tables
    WHERE table_schema = 'DB'
    AND table_name = tableName;

    IF FoundCount = 1 THEN SET @sql = CONCAT('INSERT INTO ',tableName,'
        (timestamp, lattitude, longitude,systime) 
    VALUES
        (','ts','lat','lon','systs')');
PREPARE stmt FROM @sql; 
    EXECUTE stmt;

ELSE 
    SET @sql = CONCAT('CREATE TABLE `',tableName,'`(
        `id` bigint(20) NOT NULL DEFAULT '0', 
        `timestamp` bigint(20) NOT NULL DEFAULT '0',  
        `lattitude` float NOT NULL DEFAULT '0', 
        `longitude` float NOT NULL DEFAULT '0',
        `systime` bigint(20) DEFAULT NULL, 
        KEY `LocIdx` (`vtuId`,`timestamp`),
        KEY `SysIdx` (`vtuId`,`systime`), 
        PRIMARY KEY (id)'); 
    PREPARE stmt FROM @sql; 
    EXECUTE stmt;

    SET @sql = CONCAT('INSERT INTO ',tableName,'
            (timestamp, lattitude, longitude,systime) 
        VALUES
            (','ts','lat','lon','systs')');
    PREPARE stmt FROM @sql2; 
        EXECUTE stmt;

END 
$$

DELIMITER ;

-- 当我尝试在 Mysql 5.7 中执行此查询时出现以下错误

  ERROR 1064 (42000): You have an error in your SQL syntax; check the 
  manual that corresponds to your MySQL server version for the right 
  syntax to use near '');
  PREPARE stmt FROM @sql; 
     EXECUTE stmt;

   ELSE SET @sql = CONCAT' at line 10

任何人都可以帮助改进这个程序吗?

最佳答案

这应该有效。注意单引号的顺序,因为它可能会变得棘手。

DELIMITER $$
CREATE PROCEDURE `CreateInsertLocation` (tableName VARCHAR(255),ts BIGINT(20),systs INT(20),lat FLOAT,lon FLOAT)
    BEGIN
    DECLARE FoundCount INT;

    SELECT COUNT(1) INTO FoundCount
    FROM information_schema.tables
    WHERE table_schema = 'DB'
    AND table_name = tableName;

    IF FoundCount = 1 THEN 
        SET @sql = CONCAT('INSERT INTO ',tableName,' (timestamp, lattitude, longitude,systime) VALUES (',ts, ',', lat, ',', lon, ',', systs, ')' );

        PREPARE stmt FROM @sql; 
        EXECUTE stmt;

    ELSE 
        SET @sql = CONCAT('CREATE TABLE `',tableName,'` (
        `id` bigint(20) NOT NULL DEFAULT 0, 
        `timestamp` bigint(20) NOT NULL DEFAULT 0,  
        `lattitude` float NOT NULL DEFAULT 0, 
        `longitude` float NOT NULL DEFAULT 0,
        `systime` bigint(20) DEFAULT NULL, 
        KEY `LocIdx` (`vtuId`,`timestamp`),
        KEY `SysIdx` (`vtuId`,`systime`), 
        PRIMARY KEY (id) )' ); 

        PREPARE stmt FROM @sql; 
        EXECUTE stmt;
    END IF;

    SET @sql = CONCAT('INSERT INTO ',tableName,' (timestamp, lattitude, longitude,systime) VALUES (',ts, ',', lat , ',', lon, ',', systs, ')');

    PREPARE stmt FROM @sql; 
        EXECUTE stmt;

END 
$$

DELIMITER ;

关于Mysql 5.7 存储过程报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49981501/

相关文章:

MySQL 终止连接

javascript - 如何通过动态表单获取所选ID的列?

php - 合并mysql中的依赖查询

mysql - 存储过程权限错误

mysql - MySQL 中带游标的存储过程

mysql存储函数: how to select multiple results and process them

mysql - 在表上执行 CREATE TRIGGER 时如何删除默认的 DEFINER?

php - Doctrine DBAL 对存储过程的支持

创建函数时MySQL错误1064

sql - 使用函数进行查询比使用子选择进行相同查询花费的时间更长