Примите утверждение:
MERGE INTO destination dst
USING source src
ON ( dst.some_columns = src.some_columns )
WHEN MATCHED THEN
UPDATE SET columns_to_update = dst.columns_to_update_from
WHEN NOT MATCHED THEN
INSERT ( some_columns, columns_to_update )
VALUES ( dst.some_columns, dst.columns_to_update_from )
Это может быть FULL OUTER JOIN
:
SELECT COALESCE( dst.some_columns, src.some_columns ) AS some_columns,
NVL2( dst.some_columns, dst.columns_to_update_from, columns_to_update )
AS columns_to_update
FROM source src
FULL OUTER JOIN destination dst
ON ( dst.some_columns = src.some_columns )
Или это могут быть LEFT OUTER JOIN
и UNION
:
-- WHEN MATCHED OR WHEN NOT MATCHED IN DESTINATION
SELECT src.some_columns,
NVL2( dst.some_columns, dst.columns_to_update_from, columns_to_update )
AS columns_to_update
FROM source src
LEFT OUTER JOIN destination dst
ON ( dst.some_columns = src.some_columns )
UNION ALL
-- WHEN NOT MATCHED IN SOURCE
SELECT some_columns,
columns_to_update_from
FROM destination dst
WHERE NOT EXISTS ( SELECT 1
FROM source src
WHERE ( dst.some_columns = src.some_columns ) )
Или это могут быть INNER JOIN
и UNION
s:
-- WHEN MATCHED
SELECT src.some_columns,
dst.columns_to_update_from AS columns_to_update
FROM source src
INNER JOIN destination dst
ON ( dst.some_columns = src.some_columns )
UNION ALL
-- WHEN NOT MATCHED IN DESTINATION
SELECT some_columns,
columns_to_update
FROM source src
WHERE NOT EXISTS ( SELECT 1
FROM destination dst
WHERE ( dst.some_columns = src.some_columns ) )
UNION ALL
-- WHEN NOT MATCHED IN SOURCE
SELECT some_columns,
columns_to_update_from
FROM destination dst
WHERE NOT EXISTS ( SELECT 1
FROM source src
WHERE ( dst.some_columns = src.some_columns ) )