developer
developer

Reputation: 2050

how to set triggers with the dynamic values in mysql?

I have a table A that has certain records with some value fields which gets updated on a regular basis and then i have a form that takes a value from the user. Now i have to set a trigger on that table which runs and insert a data to a different table B only when table A's field-value reaches the user-entered value.

something like this --

DELIMITER $$
 CREATE TRIGGER data_gen
 AFTER UPDATE ON TABLE A
 FOR EACH ROW BEGIN
 IF NEW.field_val ='user-entered-value' THEN
 INSERT INTO TABLE B
 SET ...........;
END IF;
END$$
 DELIMITER ;

Now how to set this trigger with user-enetered values???? Im new to Triggers and dont have idea to set triggers with the dymanic values.How can i do that???? plz help !!

Upvotes: 0

Views: 1455

Answers (1)

developer
developer

Reputation: 2050

I did it by using stored procedures.

Trigger was like --

DELIMITER $$
 CREATE TRIGGER tigger_name
 AFTER UPDATE ON table name
 FOR EACH ROW BEGIN
CALL stored_proc(OLD.field1, NEW.field2);
END$$
 DELIMITER ;

and procedure was like --

DELIMITER $$
 DROP PROCEDURE IF EXISTS stored_proc$$
 CREATE PROCEDURE stored_proc(IN var1 INT,IN var2 INT)
 BEGIN
DECLARE  no_more_products INT DEFAULT 0;
DECLARE  id_u1 INT;
DECLARE  cur_rows CURSOR FOR 
 SELECT field1 from tableb where field3=var1 and field4=var2;
   DECLARE  CONTINUE HANDLER FOR NOT FOUND 
 SET  no_more_products = 1;
 OPEN  cur_rows;
 FETCH  cur_rows INTO rows;
 REPEAT
 IF  rows > 0 THEN
 INSERT INTO ......... ;
END IF;
 FETCH  cur_rows INTO rows;
 UNTIL  no_more_products = 1
 END REPEAT;
 CLOSE  cur_rows;
END$$
 DELIMITER ;

Thanks all !

Upvotes: 1

Related Questions