Getting error on following trigger:
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 ;
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near ':NEW.affiliate_id; SELECT CUSTOMERLEVEL(:NEW.affiliate_id) INTO
vCNT;
IF (vC' at line 10
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 ;
Try above code.
One more thing you can't use NEW row for BEFORE UPDATE TRIGGER.So i had made changes to AFTER UPDATE TRIGGER.
We don't need : for NEW and OLD value,you can directly check that value using NEW.VAL and OLD.VAL.
Also whenever you use IF IN TRIGGER,PROCEDURE and FUNCTION put THEN and then write you logic
Hope this will help you.
Remove the ":" to pass the parameter NEW.affiliate_id to the CUSTOMERLEVEL function like this:
SELECT CUSTOMERLEVEL(NEW.affiliate_id) INTO vCNT
Related
I try to create a stored procedure with an if statement within.
I copied from: https://dev.mysql.com/doc/refman/5.7/en/local-variable-scope.html
But I get the following error exact on the END IF; near '':
DROP PROCEDURE IF EXISTS `myProc`;
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;
Has anyone an idea where I make a mistake?
Exact error message:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near '' at line 19 SQL Error
MySQL Version:
5.5.46
Thanks for help!
I found the solution.
I have to set DELIMITER $$ at first statement and at the end DELIMITER ;
DELIMITER $$;
DROP PROCEDURE IF EXISTS `myProc`; $$
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;$$
DELIMITER ;
It is important to set the keyword on the first position in line. If there is a blank on the first position, the error above will be thrown.
Well you are missing the loop label while ending loop. Change it to below
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP read_loop;
SELECT tmp_courseId, in_userId;
I have my procedure and is working, but my question is the following,
with the cursor is working correctly, but before the cursor I need a Single Record with several columns, I donĀ“t know if I need another cursor just for one record.
Which would be the right way to get the columns of that single row without a cursor.
The query to execute is:
'SELECT id,anio,fec_iniciointeres FROM mytable WHERE id=3 '
DELIMITER $$
DROP PROCEDURE IF EXISTS db.cal_intereses$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `cal_intereses`()
BEGIN
DECLARE factura_id INT UNSIGNED;
DECLARE val_avaluo DECIMAL(16,2);
DECLARE fec_actual DATE;
DECLARE done INT;
DECLARE cur CURSOR FOR SELECT fac_facturas.id AS factura_id, fac_facturas.val_avaluo FROM fac_facturas WHERE fac_facturas.vigencia_id<=26 AND fac_facturas.estado=1 AND fac_facturas.val_avaluo>0 LIMIT 10;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
SET fec_actual=(SELECT CURDATE());
SET done = 0;
OPEN cur;
ciclo: LOOP
FETCH cur INTO factura_id,val_avaluo;
IF done=1 THEN LEAVE ciclo; END IF;
DELETE FROM val_interesaux;
IF fec_actual>='2006-07-29' THEN
INSERT INTO val_interesaux(factura_id,fec_inicio) VALUES(factura_id,fec_actual);
END IF;
END LOOP ciclo;
CLOSE cur;
END$$
DELIMITER ;
Can someone kindly tell me where I'm wrong ?
This procedure returns an error near
#recuperato = #recuperato - saldofattura;
I mistake to update the variable #recuperato?
Thanks to all
DELIMITER //
DROP PROCEDURE IF EXISTS fatture_lettere_retail//
CREATE PROCEDURE fatture_lettere_retail (idcontratto INT(11))
BEGIN
DECLARE finito INT default 0;
DECLARE idfattura INT default 0;
DECLARE saldofattura DECIMAL(10,2);
DECLARE cur1 CURSOR FOR SELECT idfattura,saldofattura FROM fatture_lettere_isa;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
SET finito = 1;
SET #recuperato=(SELECT SUM(valorea)-SUM(valorer) FROM ImportiContratto WHERE idcontratto=idcontratto AND idimporto=1);
ciclo: LOOP
SET finito = 0;
FETCH cur1 INTO idfattura,saldofattura;
IF finito THEN
LEAVE ciclo;
END IF;
IF (#recuperato-saldofattura>=0) THEN
#recuperato = #recuperato-saldofattura;
DELETE FROM fatture_lettere_isa WHERE idfattura=idfattura;
ELSE
UPDATE fatture_lettere_isa SET saldofattura=saldofattura-#recuperato;
LEAVE ciclo;
END IF;
END LOOP ciclo;
CLOSE cur1;
END; //
DELIMITER;
Add the SET keyword at the beginning of the row in question:
SET #recuperato = #recuperato-saldofattura;
You have to write SET keyword to assign value to variable "#recuperato" as follow:
SET #recuperato = #recuperato-saldofattura;
This is my current trigger:
DELIMITER $$
CREATE DEFINER=`root`#`127.0.0.1` TRIGGER `unique_visit_new_campaign` AFTER INSERT ON `unique_visit` FOR EACH ROW
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE pixel_id int;
DECLARE campaign_id varchar(45);
DECLARE cur CURSOR FOR SELECT id FROM pixels;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
ins_loop: LOOP
FETCH cur INTO pixel_id;
IF done THEN
LEAVE ins_loop;
END IF;
INSERT IGNORE INTO pixels_campaign (campaign_id , pixel_id , date) VALUES (NEW.campaign_id ,pixel_id, current_timestamp);
END LOOP;
CLOSE cur;
END
I need it to NOT TRIGGER when new.campaign_id is empty or equals to the string {campaign_id}
I tried using MySQL IF but with no success.
Also, It kinda auto increment even when there is a campaign id already (when it ignores). any way I can stop that from happening?
Did you try this?
DELIMITER $$
CREATE DEFINER=`root`#`127.0.0.1` TRIGGER `unique_visit_new_campaign` AFTER INSERT ON `unique_visit` FOR EACH ROW
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE pixel_id int;
DECLARE campaign_id varchar(45);
DECLARE cur CURSOR FOR SELECT id FROM pixels;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
IF new.campaign = '' or new.campaign = '{campaign_id}' or new.campaign is null THEN
OPEN cur;
ins_loop: LOOP
FETCH cur INTO pixel_id;
IF done THEN
LEAVE ins_loop;
END IF;
INSERT IGNORE INTO pixels_campaign (campaign_id , pixel_id , date) VALUES (NEW.campaign_id ,pixel_id, current_timestamp);
END LOOP;
CLOSE cur;
END IF
END
My stored procedure is like this ...
DELIMITER $$
DROP PROCEDURE IF EXISTS `tds_dev`.`BlockTokenSheduler`$$
CREATE PROCEDURE `BlockTokenSheduler`(cdate date,shift varchar(20))
BEGIN
declare lo_SERIALNO int;
declare lo_TOKENNUMBER int;
declare lo_ARRIVALTIME time;
declare lo_ADJUSTMENTTIME time;
declare lo_APPOINTMENTTIME time;
declare bt_ADJUSTMENTTIME time;
declare bt_NEXTAPPOINTMENTTIME time;
declare lo_CONSULTATIONTYPE varchar(20);
declare lo_NEXTAPPOINTMENTTIME time;
declare lo_CONSULTATIONSTATUS varchar(20);
declare lo_ACTUALFINISHEDTIME time;
declare lo_SMSSTATUS varchar(20);
declare temp_appTime time;
declare time_diff time;
declare done int;
declare btdone int;
declare btcount int;
declare co int;
Declare btcountcur Cursor for
select ADJUSTMENTTIME,NEXTAPPOINTMENTTIME from tds_tokengeneration where TOKENDATE =cdate and SHIFTID = shift and blockstatus='BT' ORDER BY APPOINTMENTTIME ;
declare continue handler for not found set btdone=1;
open btcountcur;
bt_loop :LOOP
if btdone=1 then
leave bt_loop;
end if;
FETCH btcountcur into bt_ADJUSTMENTTIME,bt_NEXTAPPOINTMENTTIME;
Declare mycur cursor for
select TOKENNUMBER,APPOINTMENTTIME,ADJUSTMENTTIME,CONSULTATIONTYPE,NEXTAPPOINTMENTTIME,CONSULTATIONSTATUS,SMSSTATUS from tds_tokengeneration
where TOKENDATE=cdate and SHIFTID=shift and blockstatus='MT';
declare continue handler for not found set done=1;
open mycur;
time_loop :LOOP
FETCH mycur into lo_TOKENNUMBER,lo_APPOINTMENTTIME,lo_ADJUSTMENTTIME,
lo_CONSULTATIONTYPE,lo_NEXTAPPOINTMENTTIME,lo_CONSULTATIONSTATUS,lo_SMSSTATUS;
if done=1 then
leave time_loop;
end if;
if (lo_ADJUSTMENTTIME >= bt_ADJUSTMENTTIME and lo_APPOINTMENTTIME <= bt_NEXTAPPOINTMENTTIME) or (lo_NEXTAPPOINTMENTTIME >= bt_ADJUSTMENTTIME and lo_NEXTAPPOINTMENTTIME <= bt_NEXTAPPOINTMENTTIME)then
set lo_ADJUSTMENTTIME=bt_NEXTAPPOINTMENTTIME;
if lo_CONSULTATIONTYPE='C' then
set lo_NEXTAPPOINTMENTTIME = ADDTIME(lo_ADJUSTMENTTIME,'00:12:00');
else
set lo_NEXTAPPOINTMENTTIME = ADDTIME(lo_ADJUSTMENTTIME,'00:20:00');
end if;
update tds_tokengeneration set ADJUSTMENTTIME=lo_ADJUSTMENTTIME,
NEXTAPPOINTMENTTIME=lo_NEXTAPPOINTMENTTIME,
SMSSTATUS=lo_SMSSTATUS
where TOKENNUMBER=lo_TOKENNUMBER and TOKENDATE=cdate and SERIALNO=lo_SERIALNO;
end if;
end loop time_loop;
close mycur;
end loop bt_loop;
close btcountcur;
END$$
DELIMITER ;
.but when i executing this program i'm getting below error
(0 row(s)affected)
(0 ms taken)
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Declare mycur cursor for
select TOKENNUMBER,APPOINTMENTTIME,ADJ' at line 33
(0 ms taken)
Declarations must follow a certain order.
It is not allowed to declare a cursor or an event handler in the middle of your procedure. Yes, the error message is misleading to say the least. You must declare the mycur cursor at the beginning of a BEGIN ... END block.
You could either move the second cursor declaration to the beginning of your procedure, or nest a BEGIN ... END block at an appropriate location.