0

我尝试使用以下查询更新表中的列。在这种情况下,更新了 32000 行。

Update Table72D012 t2 set DEFINING_ARGUMENT_ID = 
(
SELECT   t3.ID
FROM     Table70t3 t3,
         Table70t1 t1,
         Table70t0 t0,         
         Table4 t4
WHERE    t2.Model_id = t4.ID
AND      t3.ID = t1.PARAM_VALUE_BPVA_ID
AND      t3.VALUE_BPAR_ID = t0.ID
AND      t0.KEY='SPECIAL_KEY'
AND      t1.LANGUAGE='en'
AND      t4.Model = t1.name

);

我想要检查结果,我使用以下查询来获取相关 ID 的计数。我得到了结果 26500 .. 为什么结果(更新计数和选择结果计数)彼此不同?
如何更正 UPDATE 查询?

只选择

SELECT   t3.ID
FROM     Table70t3 t3,
         Table70t1 t1,
         Table70t0 t0,
         Table72D012 t2,
         Table4 t4
WHERE    t2.Model_id = t4.ID
AND      t3.ID = t1.PARAM_VALUE_BPVA_ID
AND      t3.VALUE_BPAR_ID = t0.ID
AND      t0.KEY='SPECIAL_KEY'
AND      t1.LANGUAGE='en'
AND      t4.Model = t1.name
4

1 回答 1

3

首先,您需要学习正确的连接语法。在 WHERE 子句中进行连接是一个坏主意。

原因很简单。第一个查询正在更新表中的非常行,因为您没有 WHERE 子句或其他条件。

第二个查询返回与查询匹配的所有行。在更新版本中,这些行将获得 NULL 值。

这是很好的语法,MS SQL 支持但 DB2 不支持:

Update Table72D012 t2
    set DEFINING_ARGUMENT_ID = t3.ID
    from (SELECT t4.id as t4_id, t3.ID
          FROM Table70t3 t3 join 
               Table70t1 t1
               on t3.ID = t1.PARAM_VALUE_BPVA_ID join
               Table70t0 t0
               on t3.VALUE_BPAR_ID = t0.ID join
               Table4 t4 
               on t4.Model = t1.name
          WHERE t0.KEY='SPECIAL_KEY' AND t1.LANGUAGE='en'
          ) t
    WHERE t2.Model_id = t4_ID

我认为 DB2 不允许在 UPDATE 中使用 FROM 子句,因此您需要这样的内容:

Update Table72D012 t2
    set DEFINING_ARGUMENT_ID = 
         (SELECT t3.ID
          FROM Table70t3 t3 join 
               Table70t1 t1
               on t3.ID = t1.PARAM_VALUE_BPVA_ID join
               Table70t0 t0
               on t3.VALUE_BPAR_ID = t0.ID join
               Table4 t4 
               on t4.Model = t1.name
          WHERE t0.KEY='SPECIAL_KEY' AND t1.LANGUAGE='en' and t2.Model_id = t4.ID
          ) 
    WHERE exists              
         (SELECT t3.ID
          FROM Table70t3 t3 join 
               Table70t1 t1
               on t3.ID = t1.PARAM_VALUE_BPVA_ID join
               Table70t0 t0
               on t3.VALUE_BPAR_ID = t0.ID join
               Table4 t4 
               on t4.Model = t1.name
          WHERE t0.KEY='SPECIAL_KEY' AND t1.LANGUAGE='en' and t2.Model_id = t4.ID
          ) 
于 2012-06-29T16:38:07.257 回答