DWOperations
 usp_DimCheckComponentTypeLoad_DupKeyCheck (Stored Procedure)
  Properties
Property Value
Name usp_DimCheckComponentTypeLoad_DupKeyCheck
Schema dbo
Is Encrypted False
Ansi Nulls Status True
Quoted Identifier Status True
Description
  Parameters
Name Data Type Direction Description
  Parent Dependencies (objects that usp_DimCheckComponentTypeLoad_DupKeyCheck depends on)
Name Type
Table
  Child Dependencies (objects that depend on usp_DimCheckComponentTypeLoad_DupKeyCheck)
Name Type
TABLE
  Extended Properties
Object Property Value
   Annotations
Object Property Value
  DDL
/****** Object: StoredProcedure [dbo].[usp_DimCheckComponentTypeLoad_DupKeyCheck] Script Date: 03/09/2017 17:21:49 ******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE PROC [dbo].[usp_DimCheckComponentTypeLoad_DupKeyCheck]
AS 
SET NOCOUNT ON
-- Create in-memory table for later use within this proc
CREATE TABLE #CheckComponentType_Dup
    (
    [CheckComponentTypeAK] VARCHAR(18) NOT NULL
    )
-- populate table with duplicate CheckComponentTypeAK values
INSERT INTO #CheckComponentType_Dup
    (
    [CheckComponentTypeAK]
    )
    select CheckComponentTypeAK
    from Verify_dbo_DimCheckComponentType
    group by CheckComponentTypeAK
    having COUNT(CheckComponentTypeAK) > 1
    
--Detect duplicate records and set flag
UPDATE Verify_dbo_DimCheckComponentType
SET RowIsDuplicate = 'Y'
FROM Verify_dbo_DimCheckComponentType
WHERE CheckComponentTypeAK
IN
 (
 select CheckComponentTypeAK
 from #CheckComponentType_Dup
 );
--Set RowIsSelected = N for duplicated records (detected above)
UPDATE RecordsToIgnore
SET RowIsSelected = 'N'
FROM dbo.Verify_dbo_DimCheckComponentType RecordsToIgnore
INNER JOIN
 (
 --Returns the first record for a set of duplicated OrgUnit
 --(as defined as minimum of surrogate key of temp table)
 SELECT CheckComponentTypeAK, MIN(VerifyCheckComponentTypeSK) as Min_VerifyCheckComponentTypeSK
 FROM Verify_dbo_DimCheckComponentType
 WHERE CheckComponentTypeAK in
  (
    select CheckComponentTypeAK
    from #CheckComponentType_Dup
  ) 
 GROUP BY CheckComponentTypeAK 
 ) as Keeper
ON (
 Keeper.CheckComponentTypeAK = RecordsToIgnore.CheckComponentTypeAK
 AND
 Keeper.Min_VerifyCheckComponentTypeSK <> RecordsToIgnore.VerifyCheckComponentTypeSK
 )
WHERE RecordsToIgnore.RowIsDuplicate = 'Y';
Powered by BI Documenter