开发者

sql find and replace

So i'm a pretty go开发者_如何学Good systems admin and can manage mysql servers with no issue. My problem is in actually coding sql, and more specifically coding sql I feel safe to automate in nightly scripts. Thats where you awesome stack overflow guys come in.

I currently have my production wordpress site sync to my dev server so we have as close to live for testing on before a push to production. Right after I update the mysql to be a copy of production I need to find and replace about 2,000 strings.

All I need to do is find a way to execute SQL to find and replace http://DrunkOnJudgement.com to http://dev.DrunkOnJudgement.com any place in any table.

Help me Obi wan Kenobis your my only hope.


You want to do something like this

update table_name set column_name = replace(column_name, 'http://dev.DrunkOnJudgement.com', 'http://DrunkOnJudgement.com');

this will ensure that you simply replace the text you are looking for in a specific column with the text you want it to be without changing any text around it.

so for example you could just shorten it to something like: replace(column_name, 'dev.DrunkOnJudgment.com', 'DrunkOnJudgment.com')

You can also specify a where clause so that you only replace items that contain that text so something like this:

where column_name like '%dev.DrunkOnJudgement.com%'

Ok to do something like this for all columns in all tables, basically search the entire db. You can use a statement like this:

SELECT Concat('UPDATE ', TABLE_NAME, ' SET ', COLUMN_NAME, ' = REPLACE(', COLUMN_NAME, ',''dev.DrunkOnJudgment.com'',''DrunkOnJudgment.com'')', ' WHERE ', COLUMN_NAME, ' like ''%dev.DrunkOnJudgment.com%''' ) FROM INFORMATION_SCHEMA.COLUMNS

it will output a sql statement like the one above for each column and table in the database and because you are using a replace statement if it does not find the text it does not replace anything it also ensure you only update records that actually contain that text.

So to automate this you would use a cursor, I have not tested the following code but it would look something like this:

  DECLARE done BOOLEAN DEFAULT 0;
   DECLARE sql VARCHAR(2000);

   DECLARE cmds CURSOR
   FOR
   SELECT Concat('UPDATE ', TABLE_NAME, ' SET ', COLUMN_NAME, ' = REPLACE(', COLUMN_NAME, ',''dev.DrunkOnJudgment.com'',''DrunkOnJudgment.com'')', ' WHERE ', COLUMN_NAME, ' like ''%dev.DrunkOnJudgment.com%''' ) FROM INFORMATION_SCHEMA.COLUMN;

   DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
   OPEN cmds;
   REPEAT
      FETCH cmds INTO sql;
      PREPARE stmt FROM sql;
      EXECUTE stmt;
      DROP PREPARE stmt;
   UNTIL done END REPEAT;
   CLOSE cmds;


Is this as simple as an update statement with a where clause?

update myTable
set myCol = 'http://dev.DrunkOnJudgement.com'
where myCol = 'http://DrunkOnJudgement.com'


At this point in your product's life cycle I guess it is too late to suggest working out a solution that takes away this issue entirely? For example, not using absolute URLs, or replacing the urls with some variable that is evaluated at runtime?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜