0
Customer

customer_id |   customer_name | customer_city | customer_number
---------------------------------------------------------------
   1        |   john          | sanjose       | 978234
   2        |   chris         | newyork       | 293
   3        |   mary          | madrid        | 342943
   4        |   tom           | bangkok       | 8627093
---------------------------------------------------------------

Data

data_id |   data_name  |    data_city   |   data_number |   data_cust_id |  customer_id
--------------------------------------------------------------------------------------------
   1    |   abc        |    xyz         |   990         |   1        |  NULL
   2    |   john       |    sanjose     |   978234      |   1        |  NULL
   3    |   mary       |    madrid      |   8627093     |   3        |  NULL
   4    |   tom        |    LA          |   7729        |   4        |  NULL


ActionType

action_id | action_description
-----------------------------------
1         | customer_name
2         | customer_number
3         | customer_city


DataToAction

id |    data_id | action_id
--------------------------
1  |    1       | 1
2  |    1       | 2
4  |    2       | 1
5  |    2       | 2
6  |    2       | 3
7  |    3       | 1
8  |    3       | 2
9  |    4       | 1

有4张桌子 -

  1. 客户 - 有客户数据
  2. 数据 - 从外部来源提取的原始数据(有客户数据和其他数据)
  3. ActionType - 具有将在连接条件中使用的列名
  4. DataToAction - 对于数据表中的每个原始数据行,在此处指定要在联接中使用的列。

目标- 在“数据”表中填充 customer_id。我需要这样的东西

UPDATE D
SET D.customer_id = C.customer_id
FROM Data D
INNER JOIN Customer C on D.data_cust_id = C.customer_id
WHERE *("GET THE COLUMNS TO BE MATCHED FROM DATATOACTION TABLE AND USE HERE")*

例如,对于数据 id 1,我将根据 customer_name 和 customer_number 更新 customer_id,对于数据 id 2,我将根据 customer_name、customer_number 和 customer_city 更新 customer_id,依此类推。

如何在 where 子句中为要匹配的列在不同表中指定的每一行应用动态列条件。

4

1 回答 1

0

好吧,这个问题很不清楚。你能详细说明最终的结果吗?ActionType 表的用途??

UPDATE D
SET D.customer_id = C.customer_id
FROM Data D
INNER JOIN Customer C on D.data_cust_id = C.customer_id
INNER JOIN DataToAction DA ON DA.data_id = D.data_id

于 2012-12-24T06:44:55.063 回答