mysql - mysql过程中的语法错误错误代码1064

标签 mysql sql stored-procedures

我正在尝试编写一个程序,但出现语法错误。我试图在网络的帮助下修复同样的问题,但失败了。

这是我的存储过程。请问有什么帮助吗?

场景是我试图从表 hotelings 中获取workspaceid 列值,并尝试将该值作为另一个表的列。然后我尝试用 ; 更新相同的列值对于新创建的 table2 中的 hoteling 表的给定开始和结束时间。

我的两张 table 是

CREATE TABLE `hotelings` (
  `HotelingId` int(11) NOT NULL AUTO_INCREMENT,
  `Description` longtext,
  `StartDate` datetime NOT NULL,
  `EndDate` datetime NOT NULL,
  `BookedBy` longtext,
  `BookingType` int(11) NOT NULL,
  `RepeatType` longtext,
  `RepeatDay` longtext,
  `ProjectId` int(11) NOT NULL,
  `WorkSpaceId` int(11) NOT NULL,
  `starttime` varchar(45) DEFAULT NULL,
  `endtime` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`HotelingId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;



CREATE TABLE `hotelingtime` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Time` time DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

我的程序是:

    DELIMITER $$

    CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`()
    BEGIN

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'MAX(CASE WHEN workspaceid = ''',
          workspaceid,
          ''' then "" ELSE NULL end) AS ',
          CONCAT('`',workspaceid,'`')
        )
      ) INTO @sql
    FROM sms.hotelings;

    SET @sql = CONCAT('CREATE TABLE IF NOT EXISTS table2 AS SELECT t.Time as Time, ', @sql, ' FROM sms.hotelings h, sms.hotelingtime t 
                       GROUP BY t.Time');

    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    begin
    declare num_rows int;
    declare i int;
    declare col_name varchar(50);
    declare v varchar(10); 
    DECLARE v_finished INTEGER DEFAULT 0;

    -- cursor to fetch column names
    DECLARE col_names CURSOR FOR
      SELECT column_name
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE table_name = 'table2'
      ORDER BY ordinal_position; 
        -- declare NOT FOUND handler
        DECLARE CONTINUE HANDLER 
            FOR NOT FOUND SET v_finished = 1;

        OPEN col_names;

        temp_hotelingloop: LOOP

            FETCH col_names INTO col_name;

            IF v_finished = 1 THEN 
                LEAVE temp_hotelingloop;
            END IF;

    begin
    declare starttime time;
    declare endtime time;

    -- cursor to fetch start and end for a given workspaceid
    DECLARE startendTime CURSOR FOR
      SELECT starttime, endtime from hotelings
    where workspaceid = col_name;

     OPEN startendTime;
     FETCH startendTime INTO starttime, endtime;

-- i am getting error here and not giving me the result. 
    SET @sql = CONCAT('update table2 set ''',@col_name ,''' = '';'' where time between ''',@starttime,''' and ''',@endtime,'''');
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    CLOSE startendTime;
    end;
        SET i = i + 1;  

         END LOOP temp_hotelingloop;

        CLOSE col_names;

    end;
     select * from table2;
    DROP  TABLE table2;

    END

请问有什么帮助吗?

提前致谢

最佳答案

SET @sql = CONCAT('update table2 set ''',@col_name ,''' = '';'' where time between ''',@starttime,''' and ''',@endtime,'''');

您在这里使用@col_name,但在您的声明中缺少@

declare col_name varchar(50);

关于mysql - mysql过程中的语法错误错误代码1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25152879/

相关文章:

python - 使用 sqlAlchemy 存储过程

sql - 为什么即使没有更新记录,SQL Update Top 也会明显减少锁定?

Mysql只选择有数据的列

mysql insert 子句给出#1064 错误

php - 在执行查询时继续使用 Laravel 应用程序

c# - 驱动程序和提供程序之间的区别

SQL Server 分区。为明年创建新分区

javascript - 带有 'RETURNS TABLE' 的 Postgres PLV8 函数抛出 'ERROR: ReferenceError: $1 is not defined'

Java与MySQL——涉及外键时如何设计类

php - 我的 sql 查询没有在使用 session 的服务器上执行?