Oracle UPDATE语句 - 仅在我想要嵌套子查询行时更新所有列

时间:2013-02-05 03:15:47

标签: sql oracle

只有在特定情况有效时才需要更新特定列。 但是,下面是更新我表格中的所有内容。 我正在自己嵌套表,因为我必须自动更新表。

更新所有内容的Query1:

UPDATE TestTable m 
 SET m.column1 =  'VALUE2' -- I don't need any nested column data, I specifically know the data needs to UPDATE from 'Value1' to 'Value2' for filtered rows from nested Query
 WHERE EXISTS
  (
    SELECT  1 
        FROM TestTable m1
        WHERE
        m1.column2='xyz'
        AND (
         m1.nameColumn IN ('%ME%','%MYSELF%')  --- This is not really used, I am just saying I have additional filters.        
        )
        AND ( m1.column1 = 'VALUE1' OR   m1.column1 IS NULL ) -- We need to update the records which have 'VALUE1' column1 only..  

  )

查询2:也是一样....

    UPDATE TestTable m 
     SET m.column1
     = (
        select 'VALUE2'   -- Like example1, I don't need the nested table's data at all.
-- I need the nested query to filter out rows to let the outer query know that only those rows are to be updated
           FROM TestTable m1
            WHERE
            m1.column2='xyz'
            AND (
             m1.nameColumn IN ('%ME%','%MYSELF%')  --- This is not really used, I am just saying I have additional filters.        
            )
            AND ( m1.column1 = 'VALUE1' OR   m1.column1 IS NULL ) -- We need to update the records which have 'VALUE1' column1 only..  
      )
      --WHERE m1.column1 != m.column1 --- THIS DOESN'T WORK, this is an added check that I don't update where it is not needed. It can be removed/ignored

1 个答案:

答案 0 :(得分:2)

我不明白为什么你不会在where上使用直截了当的update条款:

UPDATE TestTable m SET m.column1 = 'VALUE2'
WHERE (m.column1 IS NULL OR m.column1 = 'VALUE1')
AND m.nameColumn IN ('ME', 'MYSELF')
AND m.column2 = 'xyz';

......我错过了一些明显的东西吗?