0

我在 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该过程必须每天从 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()的情况下表示附近的错误''''

顺便说一句,我会很感激任何评论,因为这是我第一次在 mysql 中做更多的事情,而不仅仅是创建表和检索行,我将第一个承认我受到更传统的编程思维(C C++ 等)的极大影响

4

0 回答 0