从Select语句中获取值并将它们插入Temp表存储过程

时间:2015-07-14 13:09:35

标签: php mysql sql stored-procedures cursor

这是我的存储过程。

这里我想从表中选择数据,将其保存在变量中,然后插入其他变量。但是这里我的select语句有。在其中,当天的价值应来自for循环。 for循环中的含义我将获得特定日期的DAYNAME(dateTime)。在此基础上我想获取值并在insert语句中使用它。

DROP PROCEDURE IF EXISTS home_cleaner.getAvailableDateAndTime;

CREATE PROCEDURE home_cleaner.`getAvailableDateAndTime`(dateTime date)
  BEGIN
  DECLARE dateTime               datetime DEFAULT NOW();
  DECLARE dateTimeCounter        int DEFAULT 0;
  DECLARE dateTimeCounterLimit   int DEFAULT 90;
  DECLARE result                 varchar(255) DEFAULT NULL;
  DECLARE result1                varchar(255) DEFAULT NULL;
  DECLARE result2                varchar(255) DEFAULT NULL;
  DECLARE result3                varchar(255) DEFAULT NULL;
  DECLARE result4                varchar(255) DEFAULT NULL;

  DECLARE
     c CURSOR FOR SELECT GROUP_CONCAT(
                            cleaner_calender.from,
                            "-",
                            cleaner_calender.to)
                            AS result
                    FROM cleaner_calender
                   WHERE day = DAYNAME(dateTime) AND status = "1";



  DROP TABLE IF EXISTS foo;
  CREATE TABLE foo
  (
     id         int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
     date1      date NULL,
     dayNameP   varchar(20) NULL,
     timings    varchar(255) NULL
  )
  ENGINE = innodb;

OPEN c;
  WHILE dateTimeCounter < dateTimeCounterLimit
  DO
     // here i want to fetch result1 for the dateTime which will be incrementing
     FETCH c INTO result1;

     INSERT INTO foo(date1, dayNameP, timings)
     VALUES (dateTime, DAYNAME(dateTime), result1);

     SET dateTime = DATE_ADD(dateTime, INTERVAL 1 DAY);
     SET dateTimeCounter = dateTimeCounter + 1;
  END WHILE;    
 CLOSE c;
 SELECT * FROM foo;
END;

0 个答案:

没有答案