Использую PGSQL, упрощенный запрос выглядит так
WITH a
AS ( SELECT DISTINCT UNNEST(ARRAY['777', NULL]) "Ключ",
UNNEST(ARRAY['111', '222']) "Знач"
)
update "Документы"
set "Название" = "Название" || '_' || a."Знач"
FROM a
where "Id" IS NOT DISTINCT FROM a."Ключ"
returning *
WITH a
AS ( SELECT DISTINCT UNNEST(ARRAY['777', NULL]) "Ключ",
UNNEST(ARRAY['333', '444']) "Знач"
)
update "Документы"
set "Название" = "Название" || '_' || a."Знач"
FROM a
where
EXISTS (
SELECT TRUE
FROM a
WHERE "Id" = a."Ключ" and "Id" IS NOT NULL and a."Ключ" IS NOT NULL
UNION
SELECT TRUE
FROM a
WHERE "Id" IS NULL and a."Ключ" IS NULL
)
returning *