How to loop through all the tables on a database to update columns

前端 未结 3 1308
别那么骄傲
别那么骄傲 2021-01-12 03:59

I\'m trying to update a column (in this case, a date) that is present on most of the tables on my database. Sadly, my database has more than 100 tables already created and f

相关标签:
3条回答
  • 2021-01-12 04:42

    You can use SHOW TABLES command to list all tables in database. Next you can check if column presented in table with SHOW COLUMNS command. It can be used this way:

    SHOW COLUMNS FROM `table_name` LIKE `column_name`
    

    If this query returns result, then column exists and you can perform UPDATE query on it.

    Update

    You can check this procedure on sqlfiddle.

    CREATE PROCEDURE UpdateTables (IN WhereColumn VARCHAR(10),
                                   IN WhereValue VARCHAR(10),
                                   IN UpdateColumn VARCHAR(10),
                                   IN UpdateValue VARCHAR(10))
    BEGIN
      DECLARE Finished BOOL DEFAULT FALSE;
      DECLARE TableName VARCHAR(10);
    
      DECLARE TablesCursor CURSOR FOR
        SELECT c1.TABLE_NAME
        FROM INFORMATION_SCHEMA.COLUMNS c1
          JOIN INFORMATION_SCHEMA.COLUMNS c2 ON (c1.TABLE_SCHEMA = c2.TABLE_SCHEMA AND c1.TABLE_NAME = c2.TABLE_NAME)
        WHERE c1.TABLE_SCHEMA = DATABASE()
          AND c1.COLUMN_NAME = WhereColumn
          AND c2.COLUMN_NAME = UpdateColumn;
    
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET Finished = TRUE;
    
      OPEN TablesCursor;
    
      MainLoop: LOOP
        FETCH TablesCursor INTO TableName;
        IF Finished THEN
          LEAVE MainLoop;
        END IF;
    
        SET @queryText = CONCAT('UPDATE ', TableName, ' SET ', UpdateColumn, '=', QUOTE(UpdateValue), ' WHERE ', WhereColumn, '=', QUOTE(WhereValue));
        PREPARE updateQuery FROM @queryText;
        EXECUTE updateQuery;
        DEALLOCATE PREPARE updateQuery;
      END LOOP;
    
      CLOSE TablesCursor;
    END
    

    This is just an example how to iterate through all tables in database and perform some action with them. Procedure can be changed according to your needs.

    0 讨论(0)
  • 2021-01-12 04:50

    Assuming you are using MySQL, You can use Stored Procedure.

    This post is a very helpful.

    Mysql-loop-through-tables

    0 讨论(0)
  • 2021-01-12 05:00

    One painless option would be to create a query which generates the UPDATE statements you want to run on all the tables:

    SELECT CONCAT('UPDATE ', a.table_name, ' SET date = "2016-04-20" WHERE name = "Example";')
    FROM information_schema.tables a
    WHERE a.table_schema = 'YourDBNameHere'
    

    You can copy the output from this query, paste it in the query editor, and run it.

    Update:

    As @PaulSpiegel pointed out, the above solution might be inconvenient if one be using an editor such as HeidiSQL, because it would require manually copying each record in the result set. Employing a trick using GROUP_CONCAT() would give a single string containing every desired UPDATE query in it:

    SELECT GROUP_CONCAT(t.query SEPARATOR '; ')
    FROM
    (
        SELECT CONCAT('UPDATE ', a.table_name,
                      ' SET date = "2016-04-20" WHERE name = "Example";') AS query,
            '1' AS id
        FROM information_schema.tables a
        WHERE a.table_schema = 'YourDBNameHere'
    ) t
    GROUP BY t.id
    
    0 讨论(0)
提交回复
热议问题