1

仅当特定情况有效时,我才需要更新特定列。但是,以下内容正在更新我表中的所有内容。我正在嵌套表格本身,因为我必须自行更新表格。

更新所有内容的 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
4

1 回答 1

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';

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

于 2013-02-05T03:18:41.263 回答