程序中的MySQL PREPARE和EXECUTE语句

时间:2012-04-28 14:45:00

标签: mysql

我在mysql中有一个程序,我希望每隔一段时间调用一次,为每个小时的每小时填充一个表的插槽,以便工作。表格格式如下

CREATE TABLE IF NOT EXISTS set1_dev_slot
(
id                 MEDIUMINT UNSIGNED                      NOT NULL AUTO_INCREMENT,
device             TINYINT UNSIGNED                        NOT NULL,
dev_date           DATE                                    NOT NULL,
dev_time           TIME                                    NOT NULL,
status             ENUM('FREE','PENDING','RESERVED')       NOT NULL DEFAULT 'FREE',
dev_type           ENUM('FIN','CHAL','NONE')               NOT NULL DEFAULT 'NONE',

PRIMARY KEY (id),
UNIQUE (device,dev_date,dev_time),

CONSTRAINT fk_device FOREIGN KEY (device) REFERENCES device (id)
);

所以我有一组用于设备的表格,我将其命名为set1_dev_slot。通常,tabes具有以下格式的名称:setX_devicessetX_dev_slot e.t.c. 该程序必须每天从1600到0100每小时添加记录,我只是用一些参数调用它来添加我想要的数量。目前的程序如下

delimiter //
CREATE PROCEDURE populateSlots (IN currentDateTime DATETIME, IN periodInDays INT)
BEGIN
    DECLARE iterDateTime DATETIME DEFAULT currentDateTime;
    DECLARE numDevices INT;
    SELECT COUNT(*) INTO numDevices FROM set1_devices;

    WHILE DATEDIFF(iterDateTime, currentDateTime) < periodInDays DO
    BEGIN
        DECLARE device_iter INT DEFAULT 1;

        WHILE device_iter < numDevices + 1 DO
            INSERT INTO set1_dev_slot (device,dev_date,dev_time) VALUES (device_iter, DATE(iterDateTime), TIME(iterDateTime));
            SET device_iter = device_iter + 1;
        END WHILE;

        SET iterDateTime = DATE_ADD(iterDateTime, INTERVAL 1 HOUR);
        IF TIME(iterDateTime) = "02:00:00" THEN
            SET iterDateTime = DATE_ADD(iterDateTime, INTERVAL 14 HOUR);
        END IF;
    END;
    END WHILE;

    BEGIN
        DECLARE device_iter INT DEFAULT 1;

        WHILE device_iter < numDevices + 1 DO
            INSERT INTO set1_dev_slot (device,dev_date,dev_time) VALUES (device_iter, DATE(iterDateTime), TIME(iterDateTime));
            SET device_iter = device_iter + 1;
        END WHILE;

        SET iterDateTime = DATE_ADD(iterDateTime, INTERVAL 1 HOUR);
        SET device_iter = 1;

        WHILE device_iter < numDevices + 1 DO
            INSERT INTO set1_dev_slot (device,dev_date,dev_time) VALUES (device_iter, DATE(iterDateTime), TIME(iterDateTime));
            SET device_iter = device_iter + 1;
        END WHILE;
    END;

END;//
delimiter ;

所以我的问题是我希望将其更改为添加到我想要和/或稍后添加的表(集)。 所以我尝试了这个,但它提出了一个问题,mysql在那里帮助不大。

delimiter //
CREATE PROCEDURE populateSlots (IN currentDateTime DATETIME, IN periodInDays INT, IN arena VARCHAR(32))
BEGIN
DECLARE iterDateTime DATETIME DEFAULT currentDateTime;
DECLARE numDevices INT DEFAULT 5;


WHILE DATEDIFF(iterDateTime, currentDateTime) < periodInDays DO
BEGIN
    DECLARE device_iter INT DEFAULT 1;

    WHILE device_iter < numDevices + 1 DO
        SET @sql_text = concat('INSERT INTO ', arena, ' (device,dev_date,dev_time) VALUES (',device_iter,', DATE("',iterDateTime,'"), TIME("',iterDateTime,'");');
        PREPARE stmt FROM @sql_text;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
        SET device_iter = device_iter + 1;
    END WHILE;

    SET iterDateTime = DATE_ADD(iterDateTime, INTERVAL 1 HOUR);
    IF TIME(iterDateTime) = "02:00:00" THEN
        SET iterDateTime = DATE_ADD(iterDateTime, INTERVAL 14 HOUR);
    END IF;
END;
END WHILE;

BEGIN
    DECLARE device_iter INT DEFAULT 1;

    WHILE device_iter < numDevices + 1 DO
        SET @sql_text = concat('INSERT INTO ', arena, ' (device,dev_date,dev_time) VALUES (',device_iter,', DATE("',iterDateTime,'"), TIME("',iterDateTime,'");');
        PREPARE stmt FROM @sql_text;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
        SET device_iter = device_iter + 1;
    END WHILE;

    SET iterDateTime = DATE_ADD(iterDateTime, INTERVAL 1 HOUR);
    SET field_iter = 1;

    WHILE device_iter < numDevices + 1 DO
        SET @sql_text = concat('INSERT INTO ', arena, ' (device,dev_date,dev_time) VALUES (',device_iter,', DATE("',iterDateTime,'"), TIME("',iterDateTime,'");');
        PREPARE stmt FROM @sql_text;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
        SET device_iter = device_iter + 1;
    END WHILE;
END;

END;//
delimiter ;

arena是我作为参数传递的表格的名称(即set1_dev_slotset2_dev_slot等),我尝试使用和不使用" @sql_text中的日期和时间函数。如果在date()的小时部分附近出现错误,并且在''附近没有任何关于错误的内容时甚至没有任何''

BTW我会很感激任何评论,因为这是我第一次在mysql中做的事情,而不仅仅是创建表格和检索行,我将是第一个承认我受到更多tratidional编程思想影响的人(C C ++)等)

0 个答案:

没有答案