简体   繁体   中英

Update Multiple Column using a single Lookup Table

I am using Azure SQL Database to create a report using Power BI. I need to create a report in the Vietnamese language for which I have replicated my database and now trying to translate English text data into Vietnamese. To achieve this I have created a lookup table that contains English text as well as corresponding translation in two different columns. My data tables can have two or more column which contains text in English that I should look up in the lookup table to fetch Vietnamese text and replace update English data with the translated one. Data Table (I can have multiple table with English data):

+-------------+-------------+-------------+----------------+-----------------+
|   id        | eng_col1    | eng_col2    | eng_col3       |  eng_col4       |
|             |             |             |                |                 |
+----------------------------------------------------------------------------+
|   1         |  hello      |             |      hello     |     yes         |
|             |             |    no       |                |                 |
+----------------------------------------------------------------------------+
|   2         |             |   no        |    hello       |                 |
|             |  world      |             |                |     no          |
+----------------------------------------------------------------------------+
|    3        |  yes        |    hello    ||      yes      |    world        |
+-------------+-------------+------------------------------+-----------------+

Lookup Table (just one lookup table which will contain all English text present across all the tables):

+-------------+--------------
|   english   | vietnamese  |
|             |             |
+---------------------------+
|   yes       |  Đúng       |
|             |             |
+---------------------------+
|   no        |             |
|             |  Không      |
+---------------------------+
|    hello    |xin chào     |
+---------------------------+
| world       | thế giới    |
|             |             |
+-------------+-------------+

Is there any way to update all the tables in one go? Or do I need to write an update query for each table separately? TIA

Without knowing your full datamodel there are 2 things you can try

Option 1

If all the columns have the same name you can write 1 query and execute them on all tables with sp_MsForEachTable

Example:

exec sp_MsForEachTable 'UPDATE x SET Column = lt.Vietnamese FROM ? x INNER JOIN lookupTable lt on lt.English = x.Column'

Option 2

You can also generate SQL Statements based on sys.tables and sys.columns . Example:


SELECT CONCAT('UPDATE x SET ', QUOTENAME(c.Name) , ' = lt.Vietnamese FROM ' , QUOTENAME(SCHEMA_NAME(t.Schema_Id)), '.', QUOTENAME(t.Name), ' x 
INNER JOIN lookupTable lt on lt.English = x.', QUOTENAME(c.Name))
FROM sys.Tables t
INNER JOIN sys.Columns c on c.Object_Id = t.Object_Id
-- Or use a different custom condition 
WHERE t.Name IN (/*Tables you want to update*/)
AND c.Name IN (/*Columns you want to update*/)

The output of this query, you can use to execute multiple statements on multiple tables.

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