简体   繁体   English

Mysql 5.7存储过程错误

[英]Mysql 5.7 Stored Procedure error

I an trying to create stored procedure in following format : DELIMITER $$ 我试图以以下格式创建存储过程: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;

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

END 
$$

DELIMITER ;

-- When I am trying to execute this query in Mysql 5.7 I am getting following error -当我尝试在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

Can anyone help to improve this sotred procedure ? 任何人都可以帮助改善这个痛苦的过程吗?

You are using single quote (') for the concat string of your create table statement and the default values within it. 您正在对创建表语句的concat字符串及其中的默认值使用单引号(')。

Either put the default values in double quotes ("), escape them (\\') or remove them as they are all numberic types. 可以将默认值放在双引号(“)中,将其转义(\\')或删除它们,因为它们都是数字类型。

Edit: Your parameters for the insert statements are also put into quotes which looks wrong, too. 编辑:您的插入语句的参数也被放在引号,这看起来也是错误的。

This should work. 这应该工作。 Pay attention to order of single quotes as it can get tricky. 注意单引号的顺序,因为它可能会很棘手。

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 ;
create schema mine;
use mine;
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;

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

END if;
end
$$
DELIMITER ;

You can try above code; 您可以尝试上面的代码;

Firstly you need to escape all Single quote ' 首先,您需要转义所有单引号'

And you forgot to close ENDIF . 而且您忘了关闭ENDIF

In the marked comment, this code was missing a quote to complete concat correctly 在标记的注释中,此代码缺少引号以正确完成concat

$$
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',')'); /* Error in this line */
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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM