gpt4 book ai didi

mysql - 触发器中的动态名称表

转载 作者:行者123 更新时间:2023-11-30 21:50:14 25 4
gpt4 key购买 nike

有一个触发器可以将列从一个表复制到另一个表。如何将表名分配给动态变量 @nametable?表格的名称每天都会根据当前日期更改。

USE dbo;
DROP TRIGGER IF EXISTS `update_test`;

GO

DELIMITER |

CREATE TRIGGER `update_test` AFTER INSERT ON `hello_send`

FOR EACH ROW

BEGIN

DECLARE nametable VARCHAR(128);

SET @nametable =(DATE_FORMAT(NOW(),"%d%m%Y_v"));

INSERT INTO `dbo`.`nametable` SET
`TIME_` =NEW.`recorded`,
`P1` = NEW.`value1`
ON DUPLICATE KEY UPDATE
`TIME_` = NEW.`recorded`,
`P1` = NEW.`value1`;

END;

出现错误 ERROR 1146: Table 'dbo.nametable' doesn't exist

最佳答案

我决定这样做作为临时解决方案:

CREATE TRIGGER `update_test` AFTER INSERT ON `hello_send`
FOR EACH ROW
BEGIN
SET @dateNameTableNOW =(DATE_FORMAT(NOW(),"%d%m%Y_v"));
SET @dateNameTableSELECT1 = '06122017_v';
SET @dateNameTableSELECT2 = '07122017_v';
IF(@dateNameTableNOW = @dateNameTableSELECT1) THEN
INSERT INTO dbo.06122017_v SET TIME_=NEW.recorded, P1 = NEW.value1 ON DUPLICATE KEY UPDATE TIME_ = NEW.recorded, P1 = NEW.value1;
END IF;

IF(@dateNameTableNOW = @dateNameTableSELECT2) THEN
INSERT INTO dbo.07122017_v SET TIME_=NEW.recorded, P1 = NEW.value1 ON DUPLICATE KEY UPDATE TIME_ = NEW.recorded, P1 = NEW.value1;
END IF;


END;

关于mysql - 触发器中的动态名称表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com