Advertisement
anchormodeling

Example Stage (day 1): Step-by-step Script

Sep 6th, 2013
2,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.97 KB | None | 0 0
  1. -- ============== Start load data from sourcetable to anchor model ==========================
  2.  
  3. -- First create table from sourcetable with an uniqe key ====================================
  4. -- the key is defined by the source system ==================================================
  5. -- in this case, location is the business key. A stage may change name, but not location. ===
  6.  
  7. SELECT  [name] as StageName,
  8.     [address] as StageAddress,
  9.     NULL AS ST_ID_Known,
  10.     NULL AS ST_ID,
  11.     [address] AS _key
  12.  
  13. INTO    #StageTable
  14.  
  15. FROM    [Stages_raw_day1]
  16.  
  17.  
  18.  
  19. -- Identify all stages that allready exists in the Anchor Database ===========================
  20.  
  21. UPDATE T
  22. SET     T.ST_ID_Known=1,
  23.     T.ST_ID=lST.ST_ID
  24.  
  25.  
  26. FROM lST_Stage lST
  27.     INNER JOIN #StageTable T ON (lST.[ST_LOC_Stage_Location]=T._key)
  28.    
  29.  
  30. -- Create new Anchors for all new Stages ======================================================
  31.  
  32. DECLARE @ids table (ST_ID int)
  33. DECLARE @NoOfKeys int
  34. DECLARE @Metadata_ID int
  35.  
  36. SET @NoOfKeys=0
  37. SET @Metadata_ID =0
  38. SELECT @NoOfKeys= COUNT(distinct _key) from #StageTable WHERE ST_ID_Known IS NULL
  39.  
  40. -- If we have new stages that should be created ===============================================
  41.  
  42. IF @NoOfKeys>0
  43.     BEGIN
  44.              -- Generate anchors
  45.                INSERT INTO @ids
  46.                EXEC kST_Stage @NoOfKeys,@Metadata_ID
  47.  
  48.  
  49.                -- Give each new anchor id to a coresponding new stage row
  50.  
  51.                 UPDATE  src
  52.  
  53.                 SET     src.ST_ID=tmpids.ST_ID,
  54.                         src.ST_ID_Known=0
  55.  
  56.                 FROM (
  57.                          -- Every new record gets a _key value  (duplicates gets the same KeyNumber) (if we have history)
  58.                          SELECT DENSE_RANK () OVER (ORDER BY _key) as KeyNumber,*
  59.                          FROM #StageTable
  60.                          WHERE ST_ID IS NULL
  61.                         )  src
  62.                              
  63.                LEFT OUTER JOIN
  64.                           (
  65.                           SELECT
  66.                           ROW_NUMBER() OVER (ORDER BY ST_ID) as KeyNumber,
  67.                           ST_ID
  68.  
  69.                           FROM @ids
  70.                           ) tmpids ON (src.KeyNumber=tmpids.KeyNumber)
  71.                
  72.     END
  73.  
  74.  
  75. -- Add attributes for the generated new anchors ================================================
  76.  
  77. INSERT INTO [dbo].[lST_Stage]
  78.            ([ST_ID]
  79.            ,[Metadata_ST]
  80.            ,[ST_NAM_Stage_Name]
  81.            ,[ST_NAM_ChangedAt]
  82.            ,[Metadata_ST_NAM]
  83.            ,[ST_LOC_Stage_Location]
  84.            ,[Metadata_ST_LOC])
  85.    
  86.  
  87.  
  88. SELECT   T.ST_ID,
  89.      @Metadata_ID,
  90.      T.StageName,
  91.      GETDATE(),
  92.      @Metadata_ID,
  93.      T.StageAddress,       
  94.      @Metadata_ID
  95.  
  96. FROM #StageTable T
  97. WHERE T.ST_ID_Known=0
  98.  
  99.  
  100. -- Change attributes for anchors where attributes differ from source (eg namechange for a stage)
  101.  
  102.  
  103. INSERT INTO [dbo].[lST_Stage]
  104.            ([ST_ID]
  105.            ,[Metadata_ST]
  106.            ,[ST_NAM_Stage_Name]
  107.            ,[ST_NAM_ChangedAt]
  108.            ,[Metadata_ST_NAM])
  109.  
  110.  
  111. SELECT  T.ST_ID,
  112.      @Metadata_ID,
  113.      T.StageName,
  114.      GETDATE(),
  115.      @Metadata_ID
  116.    
  117.  
  118. FROM    [dbo].[lST_Stage] lST
  119.     LEFT OUTER JOIN #StageTable T ON (lST.ST_ID=T.ST_ID)
  120. WHERE   lST.[ST_NAM_Stage_Name]<>T.StageName
  121. AND T.ST_ID_Known=1
  122.  
  123. DROP TABLE #StageTable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement