DWOperations
 usp_DimStudent_DupKeyCheck (Stored Procedure)
  Properties
Property Value
Name usp_DimStudent_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_DimStudent_DupKeyCheck depends on)
Name Type
Table
  Child Dependencies (objects that depend on usp_DimStudent_DupKeyCheck)
Name Type
TABLE
  Extended Properties
Object Property Value
   Annotations
Object Property Value
  DDL
/****** Object: StoredProcedure [dbo].[usp_DimStudent_DupKeyCheck] Script Date: 03/09/2017 17:21:49 ******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
/* Select and flag duplicates */
CREATE PROC [dbo].[usp_DimStudent_DupKeyCheck]
AS 
SET NOCOUNT ON
-- Create in-memory table for later use within this proc
CREATE TABLE #Stdnt_Dup
    (
    [StudentIdAK] VARCHAR(9) NOT NULL
    )
-- Populate table with duplicate StudentIdAK values
INSERT INTO #Stdnt_Dup
    (
    [StudentIDAK]
    )
    SELECT StudentIdAK
    FROM Verify_dbo_DimStudent 
    GROUP BY StudentIdAK
    HAVING COUNT(StudentIdAK) > 1
    
--Detect duplicate records and set flag
UPDATE Verify_dbo_DimStudent 
SET RowIsDuplicate = 'Y'
FROM Verify_dbo_DimStudent 
WHERE StudentIdAK 
IN
 (
 SELECT StudentIdAK
 FROM #Stdnt_Dup
 );
--Set RowIsSelected = N for duplicated records (detected above)
UPDATE RecordsToIgnore
SET RowIsSelected = 'N'
FROM dbo.Verify_dbo_DimStudent RecordsToIgnore
INNER JOIN
 (
 --Returns the first record for a set of duplicated Students
 --(as defined as minimum of surrogate key of temp table)
 SELECT StudentIdAK, MIN(VerifyStudentIdSK) as Min_VerifyStudentIdSK
 FROM Verify_dbo_DimStudent 
 WHERE StudentIdAK in
  (
    SELECT StudentIdAK
    FROM #Stdnt_Dup
  ) 
 GROUP BY StudentIdAK 
 ) AS Keeper
ON (
 Keeper.StudentIdAK = RecordsToIgnore.StudentIdAK
 AND
 Keeper.Min_VerifyStudentIdSK <> RecordsToIgnore.VerifyStudentIdSK
 )
WHERE RecordsToIgnore.RowIsDuplicate = 'Y';
Powered by BI Documenter