开发者

Trigger syntax and IF ELSE THEN

I'd like to create a trigger which count the number of rows with a specific id (id_ort). If it found more than 5 rows, I need to increment a variable.

Trigger Syntax

BEGIN
DECLARE nb INT;
DECLARE nba INT;

SET nba =0;


SET NEW.`VPLS_ID_NodeB` = CONCAT("21100", LPAD(NEW.`VPLS_ID_NodeB`,4,0));


SET nb = (SELECT COUNT(DISTINCT(`VPLS_ID_aggregation`)) FROM `VPLS_nodeB` WHERE `id_ORT` = NEW.`id_ORT`);

IF(nb > 5) THEN
SET nba = nb + 1;
ELSE
SET nba = nb;
END IF;


SET NEW.`VPLS_ID_aggregation` = CONCAT("21188", LPAD(NEW.`id_ORT`,2,0), LPAD(nba,2,0));
EN开发者_开发问答D

However, there is a bug... Even if i've less than 5 rows, the var is incremented each time.

Any ideas? Maybe it's a syntax problem...

Thanks a lot!


you probably forgot to specify a delimiter i've also made a few other changes as you can see

delimiter #

create trigger VPLS_nodeB_before_ins_trig before insert on VPLS_nodeB
for each row

BEGIN
DECLARE nb INT default 0;
DECLARE nba INT default 0;

SET NEW.VPLS_ID_NodeB = CONCAT('21100', LPAD(NEW.VPLS_ID_NodeB,4,0));
SET nb = (SELECT COUNT(DISTINCT(VPLS_ID_aggregation)) FROM VPLS_nodeB WHERE id_ORT = NEW.id_ORT);

IF(nb > 5) THEN
    SET nba = nb + 1;
ELSE
    SET nba = nb;
END IF;

SET NEW.VPLS_ID_aggregation = CONCAT('21188', LPAD(NEW.id_ORT,2,0), LPAD(nba,2,0));

END#

delimiter ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜