I know you'd have to use SHOW DATABASES, SHOW TABLES and SHOW COLUMNS, but how to use them correctly in a mysql procedure is a little out of my league. Am not sure how to check for the right datatype to match your value. Hopefully someone else will pick this up.
What means "optimum code" ? In number of code lines? In performance? In usability?
Maybe I would try to solve this not in MySQL but in plain text format on a dump file with regular expressions (grep or, in PHP, preg). This could be the optimum in coding efficiency.