2016-01-21 116 views
0

這是我的觸發器。觸發器不能始終工作

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 

我有2分型動物的情況。

的一個,其工作:(在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)(我試過ž 230000-A8754126-8B0D-4D68-A20A-7F97EDE1CE43,而不是工作壓力太大)

任何人都可以幫助嗎?你有足夠的信息能夠理解我做錯了什麼嗎?


UPDATE

我已經試過多個不同IDServerInfo

所有這些一個的工作不

Z230000-

Z23000-

ZZZZZZZ-

ZZZ111-

A123456-


那些一個工作

230000Z

1ZZZZZZ

111ZZZ

因此,如果第一個字符是字母,這是行不通的!

回答

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必須介於報價=>

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