forked from aravindsuri/ADFTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusp_Load_dimTerritory.sql
More file actions
33 lines (29 loc) · 2.52 KB
/
usp_Load_dimTerritory.sql
File metadata and controls
33 lines (29 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
USE [WineStoreDW]
GO
/****** Object: StoredProcedure [dbo].[usp_Load_dimTerritory] Script Date: 01.02.2023 15:24:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_Load_dimTerritory]
AS
BEGIN
--Implements an Upsert logic for Type 1 dimensions
--In this case the TerritoryCode is the field which is used to match the stage.Territory with dimTerritory
MERGE [dbo].[dimTerritory] AS [Target]
USING ( SELECT [TerritoryCode],
[TerritoryName],
[TradeRegion],
[Continent]
FROM [stage].[Territory]
) AS [Source]
ON [Target].[TerritoryCode] = [Source].[TerritoryCode] --- specifies the condition
WHEN MATCHED THEN
UPDATE SET [Target].[TerritoryName] = [Source].[TerritoryName],
[Target].[TradeRegion] = [Source].[TradeRegion],
[Target].[Continent] = [Source].[Continent],
[Target].[UpdatedDate] = getdate()
WHEN NOT MATCHED THEN
INSERT ([TerritoryCode], [TerritoryName], [TradeRegion], [Continent], InsertedDate, UpdatedDate)
VALUES ([Source].[TerritoryCode], [Source].[TerritoryName], [Source].[TradeRegion], [Source].[Continent], getdate(), getdate() ); --INSERT STATEMENT
END