Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE fructe (
- id INTEGER PRIMARY KEY,
- list TEXT NOT NULL
- );
- INSERT INTO fructe VALUES (1, 'mere,pere,portocale');
- INSERT INTO fructe VALUES (2, 'banane,rosii,mere');
- DELIMITER //
- CREATE PROCEDURE UpdateList()
- BEGIN
- DECLARE done INT DEFAULT 0;
- DECLARE new_list TEXT;
- DECLARE current_id INT;
- DECLARE current_list TEXT;
- DECLARE cur CURSOR FOR SELECT id, list FROM fructe;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
- OPEN cur;
- read_loop: LOOP
- FETCH cur INTO current_id, current_list;
- IF done THEN
- LEAVE read_loop;
- END IF;
- SET new_list = '';
- WHILE LOCATE(',', current_list) > 0 DO
- SET new_list = CONCAT(new_list, SUBSTRING_INDEX(current_list, ',', 1), '1,');
- SET current_list = SUBSTRING(current_list FROM LOCATE(',', current_list) + 1);
- END WHILE;
- SET new_list = CONCAT(new_list, current_list, '1');
- UPDATE fructe SET list = new_list WHERE id = current_id;
- END LOOP;
- CLOSE cur;
- END //
- DELIMITER ;
- CALL UpdateList();
- SELECT * FROM fructe;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement