で
ALTER PROCEDURE [dbo].[LOAD_DIM_ADDRESS]
AS
BEGIN
MERGE [VINCE_RETAIL_TEST].[dbo].[DIM_ADDRESS] AS T
USING (SELECT
'1' as [COMPANY_KEY],
C.[CUSTOMER_KEY],
A.[ADDRESS_ID], A.[ADDRESS_TYPE],
'0' as [REGION_KEY],
'-1' as [COUNTRY_KEY],
'-1' as [STATE_KEY],
A.[CITY], A.[POSTAL_CODE]
FROM
[AX_STAGING].[dbo].[DIM_ADDRESS] A
INNER JOIN
[VINCE_RETAIL_TEST].[dbo].[DIM_CUSTOMER] C ON C.[CUSTOMER_ID] = A.[CUSTOMER_ID]
AND C.[ADDRESS_ID] = A.[ADDRESS_ID]) AS S
ON (T.[ADDRESS_ID] = S.[ADDRESS_ID] AND T.[ADDRESS_TYPE]=S.[ADDRESS_TYPE])
WHEN MATCHED
THEN
UPDATE
SET
T.[COMPANY_KEY] = S.[COMPANY_KEY],
T.[CUSTOMER_KEY] = S.[CUSTOMER_KEY],
T.[ADDRESS_ID] = S.[ADDRESS_ID],
T.[ADDRESS_TYPE] = S.[ADDRESS_TYPE],
T.[REGION_KEY] = S.[REGION_KEY],
T.[COUNTRY_KEY] = S.[COUNTRY_KEY],
T.[STATE_KEY] = S.[STATE_KEY],
T.[CITY] = S.[CITY],
T.[POSTAL_CODE] = S.[POSTAL_CODE]
WHEN NOT MATCHED BY TARGET
THEN
INSERT ([COMPANY_KEY], [CUSTOMER_KEY], [ADDRESS_ID], [ADDRESS_TYPE],
[REGION_KEY], [COUNTRY_KEY], [STATE_KEY], [CITY],
[POSTAL_CODE])
VALUES (S.[COMPANY_KEY], S.[CUSTOMER_KEY], S.[ADDRESS_ID], S.[ADDRESS_TYPE],
S.[REGION_KEY], S.[COUNTRY_KEY], S.[STATE_KEY], S.[CITY],
S.[POSTAL_CODE]);
END
感謝。したがって、たとえば、(S.[ADDRESS_ID], S.[ADDRESS_TYPE])
がターゲット表に存在しない場合にのみ、INSERTを実行します。コードが表示されます。同様に、UPDATE部分を練習として書くことができます。 :)
INSERT [VINCE_RETAIL_TEST].[dbo].[DIM_ADDRESS]
([COMPANY_KEY], [CUSTOMER_KEY], [ADDRESS_ID], [ADDRESS_TYPE],
[REGION_KEY], [COUNTRY_KEY], [STATE_KEY], [CITY],
[POSTAL_CODE])
SELECT S.[COMPANY_KEY], S.[CUSTOMER_KEY], S.[ADDRESS_ID], S.[ADDRESS_TYPE],
S.[REGION_KEY], S.[COUNTRY_KEY], S.[STATE_KEY], S.[CITY],
S.[POSTAL_CODE]
FROM (SELECT
'1' as [COMPANY_KEY],
C.[CUSTOMER_KEY],
A.[ADDRESS_ID], A.[ADDRESS_TYPE],
'0' as [REGION_KEY],
'-1' as [COUNTRY_KEY],
'-1' as [STATE_KEY],
A.[CITY], A.[POSTAL_CODE]
FROM
[AX_STAGING].[dbo].[DIM_ADDRESS] A
INNER JOIN
[VINCE_RETAIL_TEST].[dbo].[DIM_CUSTOMER] C ON C.[CUSTOMER_ID] = A.[CUSTOMER_ID]
AND C.[ADDRESS_ID] = A.[ADDRESS_ID]) AS S
WHERE (S.[ADDRESS_ID], S.[ADDRESS_TYPE]) NOT IN (SELECT [ADDRESS_ID],[ADDRESS_TYPE] FROM [VINCE_RETAIL_TEST].[dbo].[DIM_ADDRESS])
ありがとうHenry !!それは多くの助けた –
ちょっと@SyedAqib、あなたは答えとしてそれを受け入れることができますか? –
よろしくお願いいたします。@ Dance-Henry –