Need help customizing mysql trigger - mysql

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

Related

I have problems with insert loop procedure in Mysql

I did a mysql procedure that insert in a table a number of rows for each row of other table. Thats the code:
PROCEDURE `name` (`var1` SMALLINT(6), `var2` SMALLINT(6)) BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a SMALLINT(6);
DECLARE cur CURSOR FOR SELECT id FROM tableA WHERE fk_id = var2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
END IF;
FETCH cur INTO a;
INSERT INTO tableB VALUES(var1, a);
END LOOP;
CLOSE cur;
My problem is that the last row is inserted two times. How can I resolve this?
Thanks

Stored procedure in mysql doesnt insert fetched and iterated values into table

This is what i tried. I couldnt get my values inserted into table. can anyone help me out ..
DELIMITER $$
USE `SampleDB`$$
DROP PROCEDURE IF EXISTS `Sample`$$
CREATE PROCEDURE `SampleDB`.`Sample`()
BEGIN
#declare variable
DECLARE tenantName VARCHAR(255);
DECLARE tenantAddress VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT tenant_name,tenant_address FROM tenant;
#open cursor
OPEN cur1;
#starts the loop
the_loop: LOOP
#get the values of each column into our variables
FETCH cur1 INTO tenantName,tenantAddress;
IF done THEN
LEAVE the_loop;
END IF;
#Insert it
INSERT INTO tenant(tenant_name,tenant_address)
VALUES (tenantName,tenantAddress);
END LOOP the_loop;
CLOSE cur1;
END$$
DELIMITER ;
Indeed lack 13.6.7.2. DECLARE ... HANDLER Syntax:
...
DECLARE cur1 CURSOR FOR SELECT tenant_name, tenant_address FROM tenant;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
#open cursor
...
But do you really need a cursor? With a statement like this, you can avoid the cursor:
INSERT INTO `tenant` (`tenant_name`, `tenant_address`)
SELECT `tenant_name`, `tenant_address` FROM `tenant`;

MySql Procedure not returning value

I'm unable to get the result of OUT variable in this code, am I doing wrong something? can anyone help?
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE cid INT(10);
DECLARE cuserid VARCHAR(50);
DECLARE cur1 CURSOR FOR SELECT id,username FROM tblcustomer;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO cid,cuserid;
IF done THEN
LEAVE read_loop;
END IF;
SET customers_list = CONCAT(customers_list,cid,':',cuserid,',');
END LOOP;
CLOSE cur1;
END
The procedure prototype is missing from your snippet, but assuming the below:
CREATE PROCEDURE foo(OUT customers_list VARCHAR(100))
BEGIN
...
SET customers_list = 'foo-list';
END ;
This is how you would retreive your "return value":
CALL foo(#var);
SELECT #var; -- outputs "foo-list"
Strictly speaking, a procedure has no "return value". A function has:
CREATE FUNCTION bar() RETURNS VARCHAR(100)
BEGIN
...
RETURN 'bar-list';
END ;
SELECT bar(); -- outputs "bar-list";

Using for loop in MySql data set

How to use for loop in MySQL data set ?
FOR x IN (SELECT column FROM table_name WHERE column_2 = input_val)
LOOP
sum_total := sum_total + x.column ;
END LOOP;
This is an example how I used to loop data set in oracle.
How can this be achieved in MySql ?
How about not looping at all. It's SQL after all.
SELECT SUM(`column`) total
FROM table_name
WHERE column_2 = <input_val>
Otherwise use CURSOR
Now, equivalent loop using CURSOR will look like
DELIMITER $$
CREATE PROCEDURE sp_loop(IN input_val INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE sum_current, sum_total INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT column1 FROM table_name WHERE column2 = input_val;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO sum_current;
IF done THEN
LEAVE read_loop;
END IF;
SET sum_total = sum_total + sum_current;
END LOOP;
CLOSE cur1;
SELECT sum_total;
END$$
DELIMITER ;
Here is SQLFiddle
DROP PROCEDURE IF EXISTS `foo`.`usp_cursor_example`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `foo`.`usp_cursor_example`(
IN name_in VARCHAR(255)
)
READS SQL DATA
BEGIN
DECLARE name_val VARCHAR(255);
DECLARE status_update_val VARCHAR(255);
-- Declare variables used just for cursor and loop control
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
-- Declare the cursor
DECLARE friends_cur CURSOR FOR
SELECT
name
, status_update
FROM foo.friend_status
WHERE name = name_in;
-- Declare 'handlers' for exceptions
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN friends_cur;
select FOUND_ROWS() into num_rows;
the_loop: LOOP
FETCH friends_cur
INTO name_val
, status_update_val;
IF no_more_rows THEN
CLOSE friends_cur;
LEAVE the_loop;
END IF;
select name_val, status_update_val;
-- count the number of times looped
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
-- 'print' the output so we can see they are the same
select num_rows, loop_cntr;
END
DELIMITER ;

Issues building a stored procedure with a loop using MySQL

The following procedure did not update any rows. Is there an obvious mistake in the syntax? I'm new to MySQL
set autocommit = 0;
DROP procedure IF EXISTS update_fee;
delimiter //
CREATE PROCEDURE update_fee()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE t_fee_id int(10);
DECLARE t_pubco_id int(10);
DECLARE t_auditor_id int(10);
declare fiscal_period_end_id int(10);
declare t_fiscal_year int(4);
DECLARE t_audit_id int(10);
DECLARE cur1 CURSOR FOR SELECT fee_id, pubco_id, auditor_id, fiscal_period_end_id, fiscal_year
from a_fees;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO t_fee_id, t_pubco_id,
t_auditor_id , fiscal_period_end_id, t_fiscal_year;
IF done THEN
LEAVE read_loop;
END IF;
set t_audit_id = get_audit_id (t_pubco_id,
t_auditor_id,
concat('''', t_fiscal_year, '-',
(select concat(fiscal_period_end_month, '-', fiscal_period_end_day, ''''
)
from a_fiscal_period_end
where t_fiscal_period_end_id = a_fiscal_period_end.fiscal_period_end_id
), ''''
)
);
update a_fees set audit_id = t_audit_id;
END LOOP;
CLOSE cur1;
END;
delimiter ;
set autocommit = 0; mean that you must COMMIT your changes manually. Use START TRANSACTION and COMMIT or ROLLBACK.