0

IS there a way to loop an update statement in mysql using mysql script. My code shown below

UPDATE table_name
SET column_name(REPLACE(column_name,Char(16), '')

I want that to loop from 16 to 31. Kindly advice.

I tried creating a script such as the one below

DELIMETER $$
CREATE DEFINER  `root`@`localhost` PROCEDURE `myupdate`()
 BEGIN
   DECLARE c INT;
   SET c  16;
     WHILE c < 31 DO
       UPDATE table_name
       SET column_name = replace(column_name, char(16), '');
       commit;
         SET c = c + 1;
     END WHILE;
  END

Kinda wrong if you ask me but I'm so new here in mysql. Kindly advice sir and Madam and Thanks in advance.

1 Answer 1

1

You are not using = in SET c 16;. I made some changes. Try:

DELIMETER $$
CREATE DEFINER  `root`@`localhost` PROCEDURE `myupdate`()
 BEGIN
   SET @c = 16;
     WHILE @c < 31 DO
       UPDATE table_name SET column_name = replace(column_name, char(@c), '');
       SET @c = @c + 1;
     END WHILE;
  END $$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

8 Comments

How CanI use this script sir to run for all columns inside a table not just by column. Somethng like UPDATE table_name SET (all columns) = raplce((all columns),char(c), ''); In that line
@Patrick A possible way is to list the columns like this: UPDATE table_name SET column_name_1 = replace(column_name_1, char(c), ''), column_name_2 =replace(column_name_2, char(c), ''), column_name_3 =replace(column_name_3, char(c), '');
My script does not work. :( is there a less stressing way to loop the update script?
@Patrick I would in a programming language such as PHP. It did not work, but it gave some error in MySQL?
Nah, I just need some script to run it in mysql itself. Just for studying purposes. It seems that there's more of my hair that needs pulling before I understand this one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.