mysql表触发通过错误

时间:2017-06-25 07:58:44

标签: mysql triggers

以下触发器获取错误:

DELIMITER $$
CREATE TRIGGER limit_refferals AFTER INSERT
 ON wpmr_aff_referrals
 FOR EACH ROW
 BEGIN
 DECLARE vCNT INT;
 DECLARE USERID varchar(50);
 DECLARE AFFILIATEID varchar(50);
DECLARE i INTEGER;
     DECLARE curs1 CURSOR FOR SELECT USER_ID,affiliate_id
 FROM `wpmr_aff_referrals` WHERE affiliate_id=:NEW.affiliate_id;
 SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO vCNT;
    IF (vCNT>=3)
 set i=1;
OPEN curs1;
 read_loop: LOOP
FETCH curs1 INTO USERID,AFFILIATEID;
 SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO vCNT;
IF (vCNT>=3)
set i=i+1;
ELSE
set new.affiliate_id= AFFILIATEID;
END IF;
END LOOP read_loop;
CLOSE curs1;

 END IF;

END$$
DELIMITER ;
  

您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以获得正确的语法   ':NEW.affiliate_id附近; SELECT CUSTOMERLEVEL(:NEW.affiliate_id)INTO   VCNT;       IF(第10行的vC'

2 个答案:

答案 0 :(得分:0)

删除“:”将参数NEW.affiliate_id传递给CUSTOMERLEVEL函数,如下所示:

SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT

答案 1 :(得分:0)

DELIMITER $$
CREATE TRIGGER limit_refferals BEFORE INSERT
ON wpmr_aff_referrals
FOR EACH ROW
BEGIN
    DECLARE vCNT INT;
    DECLARE USERID varchar(50);
    DECLARE AFFILIATEID varchar(50);
    DECLARE i INTEGER;
    DECLARE curs1 CURSOR FOR 
                SELECT USER_ID,affiliate_id
                FROM `wpmr_aff_referrals` WHERE affiliate_id=NEW.affiliate_id;

    SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT;
    IF (vCNT >=3) THEN    /*<------------Made changes at this line*/
        set i=1;

        OPEN curs1;
            read_loop : LOOP
            FETCH curs1 INTO USERID,AFFILIATEID;
                SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT;
                IF (vCNT>=3) THEN        /*<------------Made changes at this line*/
                    set i=i+1;
                ELSE
                    set new.affiliate_id= AFFILIATEID;
                END IF;
            END LOOP read_loop;
        CLOSE curs1;

    END IF;

END$$
DELIMITER ;

尝试以上代码。

还有一件事你无法使用NEW rowBEFORE UPDATE TRIGGER。所以我对AFTER UPDATE TRIGGER进行了更改。 对于:NEW值,我们不需要 OLD ,您可以使用NEW.VALOLD.VAL直接检查该值。 此外,无论何时使用IF IN TRIGGER PROCEDURE FUNCTION ,请执行此操作然后写下你的逻辑

希望这会对你有所帮助。