Advertisement
anchormodeling

Source to Target Generator

Apr 28th, 2017
16,722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.01 KB | None | 0 0
  1. /*
  2.     Generate source to target mapping to be used with sisula-ETL.
  3.  
  4.     VERSION HISTORY
  5.     2017-04-28  Lars Rönnbäck         CREATED
  6.     2017-05-02  Lars Rönnbäck         Added @schema.
  7.                                         Handles multiple versions.
  8.                                         Smarter code for historized columns and knots.
  9.     2017-05-04  Lars Rönnbäck         Added @naturalKeyColumns.
  10.                                         Added construct attribtues to indicate the type of the target.
  11.  
  12. */
  13.  
  14. declare @targetDatabase varchar(555) = 'DW';
  15. declare @targetSchema varchar(555) = 'dbo';
  16. declare @sourceChangingTimeColumn varchar(555) = 'Modified_Date'; -- or null for no historization
  17. declare @naturalKeyColumns varchar(555) = 'SourceCol1, SourceCol2';
  18.  
  19. if object_id('tempdb..#attributes') is not null
  20. drop table #attributes;
  21.  
  22. create table #attributes (
  23.     name varchar(555),
  24.     capsule varchar(555),
  25.     mnemonic char(3),
  26.     descriptor varchar(555),
  27.     anchorMnemonic char(2),
  28.     anchorDescriptor varchar(555),
  29.     timeRange varchar(555),
  30.     knotName varchar(555),
  31.     knotCapsule varchar(555),
  32.     knotMnemonic char(3),
  33.     knotDescriptor varchar(555),
  34.     major_id int not null primary key
  35. );
  36.  
  37. declare @sql varchar(max);
  38. set @sql = '
  39.         select
  40.             a.name,
  41.             a.capsule,
  42.             a.mnemonic,
  43.             a.descriptor,
  44.             a.anchorMnemonic,
  45.             a.anchorDescriptor,
  46.             a.timeRange,
  47.             k.name as knotName,
  48.             k.capsule as knotCapsule,
  49.             k.mnemonic as knotMnemonic,
  50.             k.descriptor as knotDescriptor,
  51.             OBJECT_ID(a.capsule + ''.'' + a.name) as major_id
  52.         from
  53.             ' + @targetSchema + '._Attribute a
  54.         left join
  55.             ' + @targetSchema + '._Knot k
  56.         on
  57.             k.mnemonic = a.knotRange
  58.         and
  59.             k.version = a.version
  60.         where
  61.             a.version = (select top 1 version from ' + @targetDatabase + '.' + @targetSchema + '._Schema order by version desc)
  62. ';
  63.  
  64. insert into #attributes exec (@sql);
  65.  
  66. with attributes as (
  67.     select
  68.         'l' + k.anchorMnemonic + '_' + k.anchorDescriptor as lView,
  69.         k.anchorMnemonic + '_' + k.anchorDescriptor as anchorName,
  70.         k.name as attributeColumn,
  71.         k.name as attributeName,
  72.         k.anchorMnemonic + '_' + k.mnemonic + '_' + k.knotName as knotColumn,
  73.         k.knotName,
  74.         case
  75.             when @sourceChangingTimeColumn is not null and k.timeRange is not null
  76.             then k.anchorMnemonic + '_' + k.mnemonic + '_ChangedAt'
  77.         end as changingColumn,
  78.         ep.*
  79.     from (
  80.         select
  81.             metadata,
  82.             sourceTable,
  83.             sourceColumn,
  84.             major_id
  85.         from -- select * from
  86.             sys.extended_properties
  87.         outer apply (
  88.             values (
  89.                 cast([value] as nvarchar(max))
  90.             )
  91.         ) d ([description])
  92.         outer apply (
  93.             values (
  94.                 left([description], patindex('%Column%', [description]) - 1),
  95.                 substring([description], patindex('%Table%', [description]) + 7, 100000000),
  96.                 substring(
  97.                     [description],
  98.                     patindex('%Column%', [description]) + 8,
  99.                     charindex(CHAR(10), [description], patindex('%Column%', [description]) + 8)
  100.                     - 1
  101.                     - (patindex('%Column%', [description]) + 8)
  102.                 )
  103.             )
  104.         ) e (metadata, sourceTable, sourceColumn)
  105.         where
  106.             [description] like '%Column%'
  107.     ) ep
  108.     join
  109.         #attributes k
  110.     on
  111.         ep.major_id = k.major_id   
  112. )
  113. select
  114.     'Autogenerated' as [@name],
  115.     @targetDatabase as [@database],
  116.     (
  117.         select
  118.             sourceTable as [@source],
  119.             lView as [@target],
  120.             anchorName as [@anchor],
  121.             (
  122.                 select
  123.                     a.sourceColumn as [@source],
  124.                     isnull(a.knotColumn, a.attributeColumn) as [@target],
  125.                     case when @naturalKeyColumns like '%' + a.sourceColumn + '%' then 'natural key' end as [@as],
  126.                     a.attributeName as [@attribute],
  127.                     a.knotName as [@knot]
  128.                 from
  129.                     attributes a
  130.                 where
  131.                     a.lView = t.lView
  132.                 and
  133.                     a.sourceTable = t.sourceTable
  134.                 for xml path('map'), type
  135.             ),
  136.             (
  137.                 select
  138.                     @sourceChangingTimeColumn as [@source],
  139.                     a.changingColumn as [@target]
  140.                 from
  141.                     attributes a
  142.                 where
  143.                     a.lView = t.lView
  144.                 and
  145.                     a.sourceTable = t.sourceTable
  146.                 and
  147.                     a.changingColumn is not null
  148.                 for xml path('map'), type
  149.             )
  150.         from
  151.             (select distinct sourceTable, lView, anchorName from attributes) t
  152.         for xml path('load'), type
  153.     )
  154. for xml path('target');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement