简体   繁体   中英

How to update all records of all tables in database?

I have a table with a lot of records (could be more than 500 000 or 1 000 000). I want to update some common columns with the same field name in all tables throughout the database.

I know the traditional way to write separate queries to individual tables but not one query to update all records of all tables.

What is the most efficient way to do this in SQL, without using some dialect-specific features, so it works everywhere (Oracle, MSSQL, MySQL, Postgres etc.)?

ADDITIONAL INFO: There are no calculated fields. There are indexes. Used generated SQL statements that update the table row by row.

(This sounds like the classic case for normalizing that 'column'.)

Anyway... No. There is no single query to locate that column across all tables, then perform an UPDATE on each of the tables.

In MySQL, you can use the table information_schema.COLUMNS to locate all the tables containing a particular named column. With such a SELECT , you can generate (using CONCAT() , etc) the desired UPDATE statements. But then, you need to manually run them (via copy and paste).

Granted, you could probably write a Stored Procedure to wrap that into a single call, but that is too risky. What if some other table has the same column name, but should not be updated?

As for common code across multiple vendors -- forget it! Virtually every syntax needs some amount of tweaking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM