特定のケースが有効な場合にのみ、特定の列を更新する必要があります。ただし、以下はテーブル内のすべてを更新しています。テーブルを自己更新する必要があるため、テーブル自体をネストしています。
すべてを更新する 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