anchormodeling

Knot Loading Generator

Apr 21st, 2017
16,980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.27 KB | None | 0 0
  1. /*
  2.     Generate a knot loading script.
  3.  
  4.     VERSION HISTORY
  5.     2017-04-24  Lars Rönnbäck         CREATED
  6.  
  7. */
  8.  
  9. declare @sourceDatabase varchar(555) = 'Staging';
  10. declare @sourceSchema   varchar(555) = 'dbo';
  11.  
  12. select
  13.     '/* ----------------------------------------------------------------------------' + CHAR(10) +
  14.     'Knot: ' + k.[name] + CHAR(10) +
  15.     ep.metadata +
  16.     '*/' +
  17.     replace(
  18.         ep.query,
  19.         sourceTable,
  20.         @sourceDatabase + '.' + @sourceSchema + '.' + sourceTable
  21.     )  as [text()]
  22. from (
  23.     select
  24.         metadata,
  25.         query,
  26.         sourceTable,
  27.         major_id
  28.     from
  29.         sys.extended_properties
  30.     cross apply (
  31.         values (
  32.             cast([value] as nvarchar(max))
  33.         )
  34.     ) d ([description])
  35.     cross apply (
  36.         values (
  37.             left([description], patindex('%Query%', [description]) - 1),
  38.             substring([description], patindex('%Query%', [description]) + 7, 100000000),
  39.             substring(
  40.                 [description],
  41.                 patindex('%Table%', [description]) + 7,
  42.                 charindex(CHAR(10), [description], patindex('%Table%', [description]) + 7)
  43.                 - 1
  44.                 - (patindex('%Table%', [description]) + 7)
  45.             )
  46.         )
  47.     ) e (metadata, query, sourceTable)
  48.     where
  49.         [description] like '%Query%'
  50. ) ep
  51. join (
  52.     select
  53.         *,
  54.         OBJECT_ID(name) as major_id
  55.     from
  56.         _Knot
  57. ) k
  58. on
  59.     ep.major_id = k.major_id
  60. for xml path('')
Add Comment
Please, Sign In to add comment