Tuesday 5 May 2020

MySQL foreach alternative for procedure

https://stackoverflow.com/questions/1775521/mysql-foreach-alternative-for-procedure

MySQL foreach alternative for procedure


Here's the mysql reference for cursors. So I'm guessing it's something like this:
  DECLARE done INT DEFAULT 0;
  DECLARE products_id INT;
  DECLARE result varchar(4000);
  DECLARE cur1 CURSOR FOR SELECT products_id FROM sets_products WHERE set_id = 1;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  OPEN cur1;

  REPEAT
    FETCH cur1 INTO products_id;
    IF NOT done THEN
      CALL generate_parameter_list(@product_id, @result);
      SET param = param + "," + result; -- not sure on this syntax
    END IF;
  UNTIL done END REPEAT;

  CLOSE cur1;

  -- now trim off the trailing , if desired

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive