DWOperations
 usp_DimZipCountyLoad_DupKeyCheck (Stored Procedure)
  Properties
Property Value
Name usp_DimZipCountyLoad_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_DimZipCountyLoad_DupKeyCheck depends on)
Name Type
Table
  Child Dependencies (objects that depend on usp_DimZipCountyLoad_DupKeyCheck)
Name Type
TABLE
  Extended Properties
Object Property Value
   Annotations
Object Property Value
  DDL
/****** Object: StoredProcedure [dbo].[usp_DimZipCountyLoad_DupKeyCheck] Script Date: 03/09/2017 17:21:49 ******/
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
/* Primary Student Term Source SELECT Statement including Incremental Modifications*/
CREATE PROC [dbo].[usp_DimZipCountyLoad_DupKeyCheck]
AS 
SET NOCOUNT ON
-- Create in-memory table for later use within this proc
CREATE TABLE #User_Dup
    (
    [ZipCodeAK] VARCHAR(5) NOT NULL
    )
-- populate table with duplicate ZipCodeAK values
INSERT INTO #User_Dup
    (
    [ZipCodeAK]
    )
    select ZipCodeAK
    from Verify_dbo_DimZipCounty
    group by ZipCodeAK
    having COUNT(ZipCodeAK) > 1
    
--Detect duplicate records and set flag
UPDATE Verify_dbo_DimZipCounty
SET RowIsDuplicate = 'Y'
FROM Verify_dbo_DimZipCounty
WHERE ZipCodeAK
IN
 (
 select ZipCodeAK
 from #User_Dup
 );
--Set RowIsSelected = N for duplicated records (detected above)
UPDATE RecordsToIgnore
SET RowIsSelected = 'N'
FROM dbo.Verify_dbo_DimZipCounty RecordsToIgnore
INNER JOIN
 (
 --Returns the first record for a set of duplicated OrgUnit
 --(as defined as minimum of surrogate key of temp table)
 SELECT ZipCodeAK, MIN(VerifyZipCountySK) as Min_VerifyZipCountySK
 FROM Verify_dbo_DimZipCounty
 WHERE ZipCodeAK in
  (
    select ZipCodeAK
    from #User_Dup
  ) 
 GROUP BY ZipCodeAK 
 ) as Keeper
ON (
 Keeper.ZipCodeAK = RecordsToIgnore.ZipCodeAK
 AND
 Keeper.Min_VerifyZipCountySK <> RecordsToIgnore.VerifyZipCountySK
 )
WHERE RecordsToIgnore.RowIsDuplicate = 'Y';
Powered by BI Documenter