Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
934 B
1
Indexable
Never
-- Обновляем agent_inn в #KP_CommonClientsOkved из #vttr
WITH CTE_UpdateAgentInn AS (
    SELECT 
        kpc.client_inn, 
        vttr.agent_inn
    FROM #KP_CommonClientsOkved kpc
    LEFT JOIN #vttr vttr ON kpc.client_inn = vttr.client_inn
    WHERE vttr.agent_inn IS NOT NULL
)
UPDATE kpc
SET kpc.agent_inn = cte.agent_inn
FROM #KP_CommonClientsOkved kpc
INNER JOIN CTE_UpdateAgentInn cte ON kpc.client_inn = cte.client_inn;

-- Обновляем client_name в #KP_CommonClientsOkved из #reg
WITH CTE_UpdateClientName AS (
    SELECT 
        kpc.client_inn, 
        reg.KONTRAGENT_NAIMENOVANIE AS client_name
    FROM #KP_CommonClientsOkved kpc
    LEFT JOIN #reg reg ON kpc.client_inn = reg.KONTRAGENT_INN
    WHERE reg.KONTRAGENT_NAIMENOVANIE IS NOT NULL
)
UPDATE kpc
SET kpc.client_name = cte.client_name
FROM #KP_CommonClientsOkved kpc
INNER JOIN CTE_UpdateClientName cte ON kpc.client_inn = cte.client_inn;
Leave a Comment