Skip to content

Instantly share code, notes, and snippets.

@noroot
Created October 17, 2017 11:20
Show Gist options
  • Save noroot/8100a18eafbe4512561513899172e5c6 to your computer and use it in GitHub Desktop.
Save noroot/8100a18eafbe4512561513899172e5c6 to your computer and use it in GitHub Desktop.
Replace all strings across database
create procedure replace_all(find varchar(255),
replce varchar(255),
indb varcv=char(255))
DECLARE loopdone INTEGER DEFAULT 0;
DECLARE currtable varchar(100);
DECLARE alltables CURSOR FOR SELECT t.tablename, c.column_name
FROM information_schema.tables t,
information_schema.columns c
WHERE t.table_schema=indb
AND c.table_schema=indb
AND t.table_name=c.table_name;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET loopdone = 1;
OPEN alltables;
tableloop: LOOP
FETCH alltables INTO currtable, currcol;
IF (loopdone>0) THEN LEAVE LOOP;
END IF;
SET stmt=CONCAT('UPDATE ',
indb, '.', currtable, ' SET ',
currcol, ' = word_sub(\'', find,
'\','\'', replce, '\') WHERE ',
currcol, ' LIKE \'%', find, '%\'');
PREPARE s1 FROM stmt;
EXECUTE s1;
DEALLOCATE PREPARE s1;
END LOOP;
END // from: https://stackoverflow.com/questions/4822638/find-and-replace-in-entire-mysql-database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment