2

继承人@Shaharsh Shah 的代码:

DELIMITER $$

DROP TRIGGER /*!50032 IF EXISTS */ trig_auto_spid$$

CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `trig_auto_spid` AFTER INSERT ON `tapes` 
FOR EACH ROW BEGIN
    SELECT REPLACE(TSpecID, 'TS', '') INTO @counter 
    FROM Tape_Specific_ID 
    ORDER BY TSpecID DESC LIMIT 1;

    SET loopCnt = @counter + new.qty; 
    WHILE (@counter <= loopCnt) DO 
        INSERT INTO Tape_Specific_ID(TapesID, TSpecID) 
        VALUES (new.TapesID, CONCAT('TS', @counter)); 
        SET @counter:=@counter + 1;
    END WHILE; 
END;
$$

DELIMITER ;

创建此触发器时发生错误 errorr 1193 (HY00):Uknown System variable 'loopCnt',为什么?任何帮助?

4

2 回答 2

2

使它成为user variable

SET @loopCnt := @counter + new.qty; 
WHILE (@counter <= @loopCnt) DO 

更新 1

SET @counter := 10;
SET @loopCnt := @counter + new.qty; 
WHILE (@counter <= @loopCnt) DO 
于 2013-01-18T09:37:00.523 回答
1

尝试

SET @loopCnt = @counter + new.qty; 
WHILE (@counter <= @loopCnt) DO 

用户定义的变量需要@在前面加上a,否则它认为它是来自系统的变量,没有定义。

于 2013-01-18T09:36:42.497 回答