触发不能一直工作

时间:2016-01-21 03:08:28

标签: mysql triggers

这是我的触发器。

CREATE TRIGGER `data_temp_after_insert` AFTER INSERT ON `data_temp`
FOR EACH ROW 

BEGIN

    DECLARE nextInc INT DEFAULT 0;  // I have try to define AS a varchar...
    DECLARE idrad varchar(75);
    DECLARE idio_bat varchar(75);
    DECLARE UniqueID varchar(75);
    DECLARE ServerID varchar(75);

    SELECT IDRadio INTO idrad FROM radio WHERE radio.XB_SL=NEW.XB_SL AND Actif=1 LIMIT 1;
    SELECT ID_IO INTO idio_bat FROM io WHERE io.IDRadio=idrad AND NoIO=1 AND Actif=1 LIMIT 1;

    SELECT IDServerInfo INTO ServerID FROM serverinfo WHERE Actif=1;

    SELECT AUTO_INCREMENT INTO nextInc FROM information_schema.tables WHERE table_name = 'io_data' AND table_schema = DATABASE( ) ;

    SET UniqueID = CONCAT(ServerID,'-',nextInc);

    IF NEW.Battery IS NOT NULL AND idio_bat IS NOT NULL AND idio_bat != 0 THEN 
        UPDATE io_data SET ValueRead=1, DateModification=now() WHERE  ID_IO=idio_bat AND ValueRead=0 AND Actif=1;
        INSERT INTO io_data (ID_IO_Data, ID_IO, ReadingValue, Timestamp) 
             VALUES (UniqueID, idio_bat, NEW.Battery, NEW.Timestamp);
    END IF;

END

这里是data_temp中的插入查询(设置了触发器)

INSERT INTO data_temp (XB_SL, HType, Battery, Temperature, Value_1, Value_2, Value_3) VALUES ('1086507849', '2', 22, -23, -0.9, -4.6, -4.9);

这里是io_data表格结构:

ID_IO_Data  varchar(75)
No      int     AUTO_INCREMENT  
ID_IO       varchar(75) 
ReadingValue    decimal(5,2)
Timestamp   timestamp   CURRENT_TIMESTAMP       
ValueRead   int

我有两种不同的情况。

正在运作的那个:(io_data中的更新以及插入内容)

IDServerInfo(定义为varchar 75)的值为230000-A8754126-8B0D-4D68-A20A-7F97EDE1CE43

因此,创建的每个ID都使用此IDServerInfo与表的自动增量连接(定义为varchar 75)。

使用此IDServerInfo在data_temp表中填充数据时,一切运行良好。

不工作的方式:

设置的唯一区别是IDServerInfo包含第一个字符的字母(例如 Z 23000-A8754126-8B0D-4D68-A20A-7F97EDE1CE43)(我试过了) Z 230000-A8754126-8B0D-4D68-A20A-7F97EDE1CE43并且无法正常工作)

任何人都可以提供帮助?你有足够的信息能够理解我做错了吗?

更新

我尝试过多种不同的IDServerInfo

所有都没有工作

Z230000 -

Z23000 -

ZZZZZZZ -

ZZZ111 -

A123456 -

那些工作

230000

230000Z

1ZZZZZZ

111ZZZ

因此,如果第一个字符是一个字母,它就不起作用了!

1 个答案:

答案 0 :(得分:0)

我不知道原因究竟是什么,但我发现错误并且它处于if条件中:

CREATE TRIGGER `data_temp_after_insert` AFTER INSERT ON `data_temp`
FOR EACH ROW 

BEGIN

DECLARE nextInc INT DEFAULT 0;  // I have try to define AS a varchar...
DECLARE idrad varchar(75);
DECLARE idio_bat varchar(75);
DECLARE UniqueID varchar(75);
DECLARE ServerID varchar(75);

SELECT IDRadio INTO idrad FROM radio WHERE radio.XB_SL=NEW.XB_SL AND Actif=1 LIMIT 1;
SELECT ID_IO INTO idio_bat FROM io WHERE io.IDRadio=idrad AND NoIO=1 AND Actif=1 LIMIT 1;

SELECT IDServerInfo INTO ServerID FROM serverinfo WHERE Actif=1;

SELECT AUTO_INCREMENT INTO nextInc FROM information_schema.tables WHERE table_name = 'io_data' AND table_schema = DATABASE( ) ;

SET UniqueID = CONCAT(ServerID,'-',nextInc);

IF NEW.Battery IS NOT NULL AND idio_bat IS NOT NULL AND idio_bat != 0 THEN 
    UPDATE io_data SET ValueRead=1, DateModification=now() WHERE  ID_IO=idio_bat AND ValueRead=0 AND Actif=1;
    INSERT INTO io_data (ID_IO_Data, ID_IO, ReadingValue, Timestamp) 
         VALUES (UniqueID, idio_bat, NEW.Battery, NEW.Timestamp);
END IF;

END

该条件为0:idio_bat!= 0必须介于quote =>之间

IF NEW.Battery IS NOT NULL AND idio_bat IS NOT NULL AND idio_bat != "0"