forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbo.usp_GetPackageDetails.sql
1505 lines (1331 loc) · 57.9 KB
/
dbo.usp_GetPackageDetails.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Author: Divya Agrawal
Original link: http://www.sqlservercentral.com/articles/Integration+Services+(SSIS)/128862/
*/
ALTER PROCEDURE [dbo].[usp_GetPackageDetails] @Path VARCHAR(8000) = 'C:\Packages1'
AS
/*
exec [usp_GetPackageDetails] 'C:\Packages1'
exec sp_configure 'show advanced options',1
reconfigure with override
go
exec sp_configure 'xp_cmdshell',1
reconfigure with override
*/
BEGIN
SET NOCOUNT ON;
--------------------drop all temp tables------------------------------------
IF OBJECT_id('pkgStats') IS NOT NULL
DROP TABLE pkgStats;
IF OBJECT_id('ConnectionDetails') IS NOT NULL
DROP TABLE ConnectionDetails;
IF OBJECT_id('PackageAnalysis') IS NOT NULL
DROP TABLE PackageAnalysis;
IF OBJECT_id('TblPrecedenceConstraint') IS NOT NULL
DROP TABLE TblPrecedenceConstraint;
IF OBJECT_id('TblControlFlowDetails') IS NOT NULL
DROP TABLE TblControlFlowDetails;
IF OBJECT_id('TblScriptTaskdetails') IS NOT NULL
DROP TABLE TblScriptTaskdetails;
IF OBJECT_id('TblExecutePackageTaskDetails') IS NOT NULL
DROP TABLE TblExecutePackageTaskDetails;
IF OBJECT_id('TblSQLTaskDetails') IS NOT NULL
DROP TABLE TblSQLTaskDetails;
IF OBJECT_id('TblDFTTaskDetails') IS NOT NULL
DROP TABLE TblDFTTaskDetails;
IF OBJECT_id('TblSrcDestDetails') IS NOT NULL
DROP TABLE TblSrcDestDetails;
IF OBJECT_id('TblDerivedTaskDetails') IS NOT NULL
DROP TABLE TblDerivedTaskDetails;
IF OBJECT_id('TblLookupTaskDetails') IS NOT NULL
DROP TABLE TblLookupTaskDetails;
IF OBJECT_id('TblVariableDetails') IS NOT NULL
DROP TABLE TblVariableDetails;
IF OBJECT_id('TblParameterDetails') IS NOT NULL
DROP TABLE TblParameterDetails;
IF OBJECT_id('tempdb.dbo.#tempprecedence1') IS NOT NULL
DROP TABLE #tempprecedence1;
IF OBJECT_id('tempdb.dbo.#tempprecedence2') IS NOT NULL
DROP TABLE #tempprecedence2;
IF OBJECT_id('tempdb.dbo.#tempprecedence3') IS NOT NULL
DROP TABLE #tempprecedence3;
IF OBJECT_id('tempdb.dbo.#tempprecedence5') IS NOT NULL
DROP TABLE #tempprecedence5;
IF OBJECT_id('tempdb.dbo.#tempprecedence5_Hierarchy') IS NOT NULL
DROP TABLE #tempprecedence5_Hierarchy;
IF OBJECT_id('tempdb.dbo.#tempprecedence4') IS NOT NULL
DROP TABLE #tempprecedence4;
IF OBJECT_id('tempdb.dbo.#tempprecedence6') IS NOT NULL
DROP TABLE #tempprecedence6;
-------------------drop all temp tables------------------------------------
------------------create all tables-------------------------------------------
CREATE TABLE pkgStats (
RowID INT identity(1, 1) NOT NULL PRIMARY KEY
,PackagePath VARCHAR(8000) NOT NULL
,PackageXML XML NOT NULL
,PackageCreatorName VARCHAR(1000) NULL
,PackageProtectionLevel varchar(100) null
);
CREATE TABLE ConnectionDetails (
ConnectionManager VARCHAR(5000)
,ConnectionManagerID VARCHAR(100)
);
CREATE TABLE PackageAnalysis (
TableRowID INT identity(1, 1) NOT NULL
,PackageSequenceNo INT
,SequenceNo INT
,RowID INT
,PackagePath VARCHAR(5000)
,PackageName VARCHAR(max)
,Category VARCHAR(1000)
,PackageCreatorName VARCHAR(1000)
,PackageProtectionLevel Varchar(100)
,TaskPath VARCHAR(max)
,ObjectName VARCHAR(500)
,ObjectType VARCHAR(max)
,ObjectTypeDescription VARCHAR(max)
,DelayValidationPropertyValue VARCHAR(10)
,ObjectValue VARCHAR(max)
,ExpressionValue VARCHAR(5000)
,ConnectionManager VARCHAR(5000)
,SourceColumn VARCHAR(5000)
,DestinationColumn VARCHAR(5000)
,SortKeyPosition varchar(10)
,DerivedValue VARCHAR(5000)
,IsDisabled VARCHAR(10)
,ResultSetParameterName VARCHAR(1000)
,ParameterBindingSequence VARCHAR(1000)
,ParameterBindingParameterName VARCHAR(1000)
--,ObjectPathFrom VARCHAR(8000)
--,ObjectPathTo VARCHAR(8000)
,ExecutePackageExpression VARCHAR(8000)
,ExecutedPackageName VARCHAR(8000)
,ExecutePackageConnection VARCHAR(8000)
,Script VARCHAR(max)
,Variable VARCHAR(1000)
,ConnectionManagerID VARCHAR(100)
,LookupJoins VARCHAR(8000)
,IsSortedProperty varchar(10)
,MultihashcolumnSortPosition varchar(max)
,RetainSameConnectionProperty varchar(100)
);
CREATE CLUSTERED INDEX CX_ControlFlow ON PackageAnalysis (TableRowID);
CREATE TABLE TblPrecedenceConstraint (
RowID INT
,PackagePath VARCHAR(2000)
,TaskFrom VARCHAR(1000)
,Taskto VARCHAR(1000)
,PrecOrder INT
,PrecOrder1 INT
,TaskType VARCHAR(1000)
);
CREATE TABLE TblVariableDetails (
VariableRowID INT identity(1, 1) NOT NULL
,RowID INT
,PackagePath VARCHAR(5000)
,PackageName VARCHAR(max)
,Category VARCHAR(1000)
,PackageCreatorName VARCHAR(1000)
,TaskPath VARCHAR(max)
,ObjectName VARCHAR(500)
,ObjectType VARCHAR(max)
,ObjectValue VARCHAR(max)
,ExpressionValue VARCHAR(5000)
,VariableHexValue VARCHAR(1000)
);
CREATE CLUSTERED INDEX CX_Variable ON TblVariableDetails (VariableRowID);
CREATE TABLE TblParameterDetails (
ParameterRowID INT identity(1, 1) NOT NULL
,RowID INT
,PackagePath VARCHAR(5000)
,PackageName VARCHAR(max)
,Category VARCHAR(1000)
,PackageCreatorName VARCHAR(1000)
,TaskPath VARCHAR(max)
,ObjectName VARCHAR(500)
,ObjectType VARCHAR(max)
,ObjectValue VARCHAR(max)
,ExpressionValue VARCHAR(5000)
,ParameterHexValue VARCHAR(1000)
);
CREATE CLUSTERED INDEX CX_Variable ON TblParameterDetails (ParameterRowID);
CREATE TABLE TblControlFlowDetails (
ControlFlowDetailsRowID INT identity(1, 1) NOT NULL
,RowID INT
,TaskPath VARCHAR(8000)
,TaskName VARCHAR(2000)
,TaskTypeDescription VARCHAR(MAX)
,TaskType VARCHAR(5000)
,DelayValidationPropertyValue VARCHAR(100)
,DFTQuery XML
,SqlTaskQry XML
,ExecPkgTaskQry XML
,ScriptTaskQry XML
,IsDisabled VARCHAR(10)
,ExpressionValue VARCHAR(5000)
);
CREATE CLUSTERED INDEX CX_ControlFlow ON TblControlFlowDetails (
ControlFlowDetailsRowID
,RowID
);
CREATE NONCLUSTERED INDEX NCX_ControlFlow ON TblControlFlowDetails (RowID);
CREATE TABLE TblScriptTaskdetails (
RowID INT
,ControlFlowDetailsRowID INT
,Script VARCHAR(max)
);
CREATE CLUSTERED INDEX CX_TblScriptTaskdetails ON TblScriptTaskdetails (
ControlFlowDetailsRowID
,RowID
);
CREATE TABLE TblExecutePackageTaskDetails (
RowID INT
,ControlFlowDetailsRowID INT
,ExecutePackageExpression VARCHAR(1000)
,ExecutedPackageName VARCHAR(1000)
,ExecutePackageConnection VARCHAR(1000)
);
CREATE CLUSTERED INDEX CX_TblExecutePackageTaskDetails ON TblExecutePackageTaskDetails (
ControlFlowDetailsRowID
,RowID
);
CREATE TABLE TblSQLTaskDetails (
RowID INT
,ControlFlowDetailsRowID INT
,ResultSetParameterName VARCHAR(1000)
,ParameterBindingSequence VARCHAR(1000)
,ParameterBindingParameterName VARCHAR(1000)
);
CREATE CLUSTERED INDEX CX_TblSQLTaskDetails ON TblSQLTaskDetails (
ControlFlowDetailsRowID
,RowID
);
CREATE TABLE TblDFTTaskDetails (
DataFlowDetailsRowID INT identity(1, 1) NOT NULL
,RowID INT
,DFTTaskName VARCHAR(max)
,DFTTasksPath VARCHAR(max)
,DFTTaskType VARCHAR(max)
,DFTTaskType1 VARCHAR(max)
,DFTRowSet VARCHAR(max)
,ParameterBindingParameterName VARCHAR(2000)
,DFTSQLCommand VARCHAR(max)
,DFTConnectionManager VARCHAR(max)
,Variable VARCHAR(max)
,InputQry XML
,OutputQry XML
,IsSortedProperty VARCHAR(10)
,MultihashcolumnSortPosition varchar(max)
);
CREATE CLUSTERED INDEX CX_TblDFTTaskDetails ON TblDFTTaskDetails (
DataFlowDetailsRowID
,RowID
);
CREATE NONCLUSTERED INDEX NCX_TblDFTTaskDetails ON TblDFTTaskDetails (RowID);
CREATE TABLE TblSrcDestDetails (
DataFlowDetailsRowID INT
,RowID INT
,SourceColumn VARCHAR(8000)
,DestinationColumn VARCHAR(8000)
,SortKeyPosition varchar(10)
);
CREATE CLUSTERED INDEX CX_TblSrcDestDetails ON TblSrcDestDetails (
DataFlowDetailsRowID
,RowID
);
CREATE TABLE TblDerivedTaskDetails (
DataFlowDetailsRowID INT
,RowID INT
,DerivedValue VARCHAR(8000)
,SourceColumn VARCHAR(8000)
,DestinationColumn VARCHAR(8000)
);
CREATE CLUSTERED INDEX CX_TblDerivedTaskDetails ON TblDerivedTaskDetails (
DataFlowDetailsRowID
,RowID
);
CREATE TABLE TblLookupTaskDetails (
DataFlowDetailsRowID INT
,RowID INT
,LookupJoins VARCHAR(8000)
,SourceColumn VARCHAR(8000)
,DestinationColumn VARCHAR(8000)
);
CREATE CLUSTERED INDEX CX_TblLookupTaskDetails ON TblLookupTaskDetails (
DataFlowDetailsRowID
,RowID
);
--------------------------create all tables-------------------------------------------
-------------------Iterate over all dtsx files from folder------------------------------
SET @Path = @Path + '\*.dtsx';
DECLARE @MyFiles TABLE (
MyID INT IDENTITY(1, 1) PRIMARY KEY
,FullPath VARCHAR(8000)
);
DECLARE @CommandLine VARCHAR(8000);
SELECT @CommandLine = LEFT('dir "' + @Path + '" /A-D /B /S ', 8000);
INSERT INTO @MyFiles (FullPath)
EXECUTE xp_cmdshell @CommandLine;
DELETE
FROM @MyFiles
WHERE FullPath IS NULL
OR FullPath = 'File Not Found'
OR FullPath = 'The system cannot find the path specified.'
OR FullPath = 'The system cannot find the file specified.';
--select * from @MyFiles
DECLARE @FullPath VARCHAR(2000);
DECLARE @counter INT = 0
,@totalpkgcount INT;
SELECT @totalpkgcount = COUNT(*)
FROM @MyFiles;
WHILE @counter <= @totalpkgcount
BEGIN
SELECT @FullPath = FullPath
FROM @MyFiles
WHERE MyID = @counter;
DECLARE @sql NVARCHAR(max);
SET @sql = '
INSERT INTO pkgStats (PackagePath,PackageXML)
select ''@FullPath'' as PackagePath
, cast(BulkColumn as XML) as PackageXML
from openrowset(bulk ''@FullPath'',
single_blob) as pkgColumn';
SELECT @sql = REPLACE(@sql, '@FullPath', @FullPath);
EXEC sp_executesql @sql;
SET @counter = @counter + 1;
END
-------------------Iterate over all dtsx files from folder------------------------------
--select * from pkgStats
------------add package creator name---------------------------
;
WITH CTE_PkgLevel
AS (
SELECT RowID
,PackagePath
,PackageXML.value('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
/DTS:Executable[1]/@DTS:CreatorName', 'nvarchar(500)') AS PackageCreatorName
,PackageXML.value('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
/DTS:Executable[1]/@DTS:ProtectionLevel', 'nvarchar(500)') AS PackageProtectionLevel
FROM pkgStats pkg
)
UPDATE P
SET PackageCreatorName = C.PackageCreatorName,
PackageProtectionLevel = C.PackageProtectionLevel
FROM pkgStats P
INNER JOIN CTE_PkgLevel C ON P.RowID = C.RowID;
------------add package creator name---------------------------
PRINT '---------------Get all connections-----------------------------------';
WITH CTE_PkgLevel
AS (
SELECT pkg.RowID
,pkg.PackagePath
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:ObjectName[1]', 'varchar(max)') AS ConnectionManagerName
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:CreationName[1]', 'varchar(max)') AS ConnectionManagerType
--, cfnodes1.y.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:ConnectionString[1]', 'varchar(max)')
,cfnodes.x.query('.').query('declare namespace p1="www.microsoft.com/SqlServer/Dts";./p1:ConnectionManager/p1:ObjectData/p1:ConnectionManager') AS ConnectionStringQry
,cfnodes.x.query('.').query('declare namespace p1="www.microsoft.com/SqlServer/Dts";./p1:ConnectionManager/p1:PropertyExpression') AS ExpressionQry
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:DTSID[1]', 'varchar(100)') AS ConnectionManagerID
,pkg.PackageCreatorName
FROM pkgStats pkg
CROSS APPLY pkg.PackageXML.nodes('declare namespace DTS="www.microsoft.com/SqlServer/Dts";(//DTS:Executable/DTS:ConnectionManagers/*)') AS cfnodes(x)
)
,CTE_PkgConLevel
AS (
SELECT pkg.RowID
,pkg.PackagePath
,pkg.ConnectionManagerName
,pkg.ConnectionManagerType
,ConnectionManagerID
--,pkg.ConnectionStringQry
,(
SELECT TOP 1 cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";@p1:ConnectionString[1]', 'nvarchar(max)') AS ConnectionString
FROM pkg.ConnectionStringQry.nodes('./*') AS cfnodes(x)
) AS ConnectionString
,(
SELECT TOP 1 cfnodes.x.value('.', 'nvarchar(max)') AS ExpressionValue
FROM pkg.ExpressionQry.nodes('./*') AS cfnodes(x)
) AS ExpressionValue
,PackageCreatorName
,(
SELECT TOP 1 cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";@p1:Retain[1]', 'nvarchar(max)') AS RetainSameConnectionProperty
FROM pkg.ConnectionStringQry.nodes('./*') AS cfnodes(x)
) AS RetainSameConnectionProperty
FROM CTE_PkgLevel pkg
)
INSERT INTO PackageAnalysis (
RowID
,PackagePath
,PackageName
,Category
,PackageCreatorName
,ObjectName
,ObjectType
,DelayValidationPropertyValue
,ObjectValue
,ExpressionValue
,ConnectionManagerID
,RetainSameConnectionProperty
)
SELECT DISTINCT pkg.RowID
,pkg.PackagePath
,REPLACE(pkg.PackagePath, REPLACE(@Path, '*.dtsx', ''), '') PackageName
,'Connection' AS CategoryC
,pkg.PackageCreatorName
,cast(pkg.ConnectionManagerName AS VARCHAR(max)) AS ObjectName
,cast(pkg.ConnectionManagerType AS VARCHAR(max)) AS ObjectType
,'NA' AS DelayValidationPropertyValue
,cast(pkg.ConnectionString AS VARCHAR(max)) AS ObjectValue
,cast(ExpressionValue AS VARCHAR(max)) AS ExpressionValue
,ConnectionManagerID
,RetainSameConnectionProperty
FROM CTE_PkgConLevel pkg;
PRINT '---------------Get all connections-----------------------------------';
PRINT '------------------------Get all variable details-------------------------------------';
WITH CTE_PkgLevel
AS (
SELECT pkg.RowID
,pkg.PackagePath
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:ObjectName[1]', 'varchar(max)') AS VariableName
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:DTSID[1]', 'varchar(max)') AS VariableHexValue
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:Expression[1]', 'varchar(max)') AS ExpressionValue
,cfnodes.x.query('.').query('declare namespace p1="www.microsoft.com/SqlServer/Dts";./p1:Variable/p1:VariableValue') AS VariableQry
,PackageCreatorName
FROM pkgStats pkg
CROSS APPLY pkg.PackageXML.nodes('declare namespace DTS="www.microsoft.com/SqlServer/Dts";(//DTS:Executable/DTS:Variables/*)') AS cfnodes(x)
)
--select * from CTE_PkgLevel
,CTE_PkgVarLevel
AS (
SELECT pkg.RowID
,pkg.PackagePath
,pkg.VariableName
,pkg.ExpressionValue
,VariableHexValue
,(
SELECT TOP 1 cfnodes.x.value('.', 'nvarchar(max)') AS VariableValue
FROM pkg.VariableQry.nodes('./*') AS cfnodes(x)
) AS VariableValue
--,pkg.ConnectionStringQry
,(
SELECT TOP 1 cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";@p1:DataType[1]', 'nvarchar(max)') AS DataType
FROM pkg.VariableQry.nodes('./*') AS cfnodes(x)
) AS VariableDataType
,PackageCreatorName
FROM CTE_PkgLevel pkg
)
INSERT INTO TblVariableDetails (
RowID
,PackagePath
,PackageName
,Category
,PackageCreatorName
,ObjectName
,ObjectType
,ObjectValue
,ExpressionValue
,VariableHexValue
)
SELECT DISTINCT Tblvar.RowID
,Tblvar.PackagePath
,REPLACE(Tblvar.PackagePath, REPLACE(@Path, '*.dtsx', ''), '') PackageName
,'Variable' AS Category
,Tblvar.PackageCreatorName
,cast(Tblvar.VariableName AS VARCHAR(max)) AS TaskName
,cast(Tblvar.VariableDataType AS VARCHAR(max)) AS TaskType
,isnull(cast(Tblvar.VariableValue AS VARCHAR(max)), '') AS VariableValue
,isnull(cast(tblvar.ExpressionValue AS VARCHAR(max)), '') ExpressionValue
,VariableHexValue
FROM CTE_PkgVarLevel Tblvar;
INSERT INTO PackageAnalysis (
RowID
,PackagePath
,PackageName
,Category
,PackageCreatorName
,ObjectName
,ObjectType
,DelayValidationPropertyValue
,ObjectValue
,ExpressionValue
)
SELECT DISTINCT Tblvar.RowID
,Tblvar.PackagePath
,PackageName
,'Variable' AS Category
,Tblvar.PackageCreatorName
,ObjectName
,ObjectType
,'NA' AS DelayValidationPropertyValue
,ObjectValue
,ExpressionValue
FROM TblVariableDetails Tblvar;
PRINT '------------------------Get all variable details-------------------------------------';
PRINT '------------------------Get all parameter details-------------------------------------';
WITH CTE_PkgLevel
AS (
SELECT pkg.RowID
,pkg.PackagePath
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:ObjectName[1]', 'varchar(max)') AS ParameterName
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:DTSID[1]', 'varchar(max)') AS ParameterHexValue
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:Expression[1]', 'varchar(max)') AS ExpressionValue
,cfnodes.x.query('.').query('declare namespace p1="www.microsoft.com/SqlServer/Dts";./p1:PackageParameter/p1:Property') AS ParameterQry
,PackageCreatorName
FROM pkgStats pkg
CROSS APPLY pkg.PackageXML.nodes('declare namespace DTS="www.microsoft.com/SqlServer/Dts";(//DTS:Executable/DTS:PackageParameters/*)') AS cfnodes(x)
)
--select * from CTE_PkgLevel
,CTE_PkgVarLevel
AS (
SELECT pkg.RowID
,pkg.PackagePath
,pkg.ParameterName
,pkg.ExpressionValue
,ParameterHexValue
,(
SELECT TOP 1 cfnodes.x.value('.', 'nvarchar(max)') AS ParameterValue
FROM pkg.ParameterQry.nodes('./*') AS cfnodes(x)
) AS ParameterValue
--,pkg.ConnectionStringQry
,(
SELECT TOP 1 cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";@p1:DataType[1]', 'nvarchar(max)') AS DataType
FROM pkg.ParameterQry.nodes('./*') AS cfnodes(x)
) AS ParameterDataType
,PackageCreatorName
FROM CTE_PkgLevel pkg
)
INSERT INTO tblParameterDetails(
RowID
,PackagePath
,PackageName
,Category
,PackageCreatorName
,ObjectName
,ObjectType
,ObjectValue
,ExpressionValue
,ParameterHexValue
)
SELECT DISTINCT Tblvar.RowID
,Tblvar.PackagePath
,REPLACE(Tblvar.PackagePath, REPLACE(@Path, '*.dtsx', ''), '') PackageName
,'Variable' AS Category
,Tblvar.PackageCreatorName
,cast(Tblvar.ParameterName AS VARCHAR(max)) AS TaskName
,cast(Tblvar.ParameterDataType AS VARCHAR(max)) AS TaskType
,isnull(cast(Tblvar.ParameterValue AS VARCHAR(max)), '') AS ParameterValue
,isnull(cast(tblvar.ExpressionValue AS VARCHAR(max)), '') ExpressionValue
,ParameterHexValue
FROM CTE_PkgVarLevel Tblvar;
INSERT INTO PackageAnalysis (
RowID
,PackagePath
,PackageName
,Category
,PackageCreatorName
,ObjectName
,ObjectType
,DelayValidationPropertyValue
,ObjectValue
,ExpressionValue
)
SELECT DISTINCT Tblvar.RowID
,Tblvar.PackagePath
,PackageName
,'Parameter' AS Category
,Tblvar.PackageCreatorName
,ObjectName
,ObjectType
,'NA' AS DelayValidationPropertyValue
,ObjectValue
,ExpressionValue
FROM tblParameterDetails Tblvar;
PRINT '------------------------Get all parameter details-------------------------------------';
PRINT '----------------Get precedence constraint details------------------------------';
WITH CTE_CFPrec
AS (
SELECT pkg.RowID
,pkg.PackagePath
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:From[1]', 'varchar(max)') AS TaskFrom
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:To[1]', 'nvarchar(max)') AS TaskTo
--,dft.x.value('@refId[1]','varchar(max)')
FROM pkgStats pkg
CROSS APPLY pkg.PackageXML.nodes('declare namespace DTS="www.microsoft.com/SqlServer/Dts";//DTS:Executable/DTS:PrecedenceConstraints/*') AS cfnodes(x)
)
,CTE_DFTPrec
AS (
SELECT pkg.RowID
,pkg.PackagePath
,cfnodes.x.value('./@startId[1]', 'varchar(max)') AS TaskFrom
,cfnodes.x.value('./@endId[1]', 'nvarchar(max)') AS TaskTo
--,dft.x.value('@refId[1]','varchar(max)')
FROM pkgStats pkg
CROSS APPLY pkg.PackageXML.nodes('declare namespace DTS="www.microsoft.com/SqlServer/Dts";//DTS:Executable/DTS:ObjectData/pipeline/paths/*') AS cfnodes(x)
)
INSERT INTO TblPrecedenceConstraint (
RowID
,PackagePath
,TaskFrom
,Taskto
,TaskType
)
SELECT RowID
,PackagePath
,TaskFrom
,TaskTo
,TaskType
FROM (
SELECT RowID
,PackagePath
,TaskFrom
,TaskTo
,'Control Flow' AS TaskType
FROM CTE_CFPrec
UNION ALL
SELECT RowID
,PackagePath
,substring(TaskFrom, 1, charindex('.', TaskFrom) - 1) AS TaskFrom
,substring(TaskTo, 1, CHARINDEX('.', TaskTo) - 1)
,'Data Flow'
FROM CTE_DFTPrec
) t;
CREATE CLUSTERED INDEX CIX_TblPrecedenceConstraint ON TblPrecedenceConstraint (RowID);
CREATE NONCLUSTERED INDEX NCIX_TblPrecedenceConstraint ON TblPrecedenceConstraint (
TaskFrom
,TaskTo
);
SELECT DISTINCT RowID,TaskFrom,IsParentLevel,Row_number() OVER (
PARTITION BY rowid ORDER BY level) level INTO #tempprecedence1
FROM
(
SELECT distinct t1.Rowid
,t1.TaskFrom
,1 AS isparentlevel
, len(t1.TaskFrom) - len(replace(t1.TaskFrom, '\', '')) AS level
FROM TblPrecedenceConstraint t1
LEFT JOIN TblPrecedenceConstraint t2 ON t1.rowid = t2.RowID
AND t1.TaskFrom = t2.taskTo
WHERE t2.taskto IS NULL
)T
CREATE CLUSTERED INDEX CIX_TblPrecedenceConstraint1 ON #tempprecedence1 (RowID);
CREATE NONCLUSTERED INDEX NCIX_TblPrecedenceConstraint1 ON #tempprecedence1 (level);
CREATE TABLE #tempprecedence2 (
ROWID INT
,TaskFrom VARCHAR(1000)
,task VARCHAR(1000)
,parentlevel INT
,LEVEL INT
);
DECLARE @cntlevel INT
,@cnt INT = 1;
SELECT @cntlevel = count(*)
FROM #tempprecedence1;
WHILE @cnt <= @cntlevel
BEGIN
;
WITH cte
AS (
SELECT rowid
,taskfrom
,taskfrom AS task
,LEVEL
,1 AS cnt
FROM #tempprecedence1
WHERE LEVEL = @cnt
UNION ALL
SELECT c.rowid
,c.taskfrom
,t.taskto
,LEVEL
,cnt + 1
FROM TblPrecedenceConstraint t
INNER JOIN cte c ON t.RowID = c.RowID
AND c.task = t.taskfrom
)
INSERT INTO #tempprecedence2 (
rowid
,taskfrom
,task
,parentlevel
,LEVEL
)
SELECT rowid
,taskfrom
,task
,@cnt
,cnt
FROM cte;
SELECT @cnt = @cnt + 1;
END
SELECT rowid
,task
,parentlevel
,LEVEL
,row_number() OVER (
PARTITION BY rowid ORDER BY parentlevel
,LEVEL
) trank
INTO #tempprecedence3
FROM #tempprecedence2;
CREATE CLUSTERED INDEX CIX_TblPrecedenceConstraint1 ON #tempprecedence3 (RowID);
CREATE NONCLUSTERED INDEX NCIX_TblPrecedenceConstraint1 ON #tempprecedence3 (task);
SELECT t.rowid
,isnull(t1.task,t.task) as task
,t.LEVEL tlevel
,isnull(max(t1.LEVEL),max(t.level)) level
,isnull(max(t1.trank),max(t.trank)) trank
INTO #tempprecedence4
FROM #tempprecedence3 t
LEFT JOIN #tempprecedence3 t1 ON t.rowid = t1.rowid
AND t1.task LIKE '%' + t.task + '\%'
GROUP BY t.rowid
,t1.task
,t.task
,t.LEVEL
--,t1.LEVEL
ORDER BY LEVEL
,trank;
--select t.rowid,t1.task,t.level tlevel,t1.level, (t1.trank) trank into #tempprecedence4
--from #tempprecedence3 t
--join #tempprecedence3 t1
--on t.rowid = t1.rowid
--and t1.task like '%'+t.task+'%'
-- order by trank
SELECT t.rowid
,t.task
,replace(replace(replace((
SELECT tlevel
FROM #tempprecedence4 t1
WHERE t1.rowid = t.rowid
AND t1.task = t.task
FOR XML path('')
), '</tlevel><tlevel>', '/'), '</tlevel>', '/'), '<tlevel>', '') sequenceno,trank
INTO #tempprecedence5
FROM #tempprecedence4 t
GROUP BY t.rowid
,t.task
,t.trank
ORDER BY rowid
,trank;
SELECT rowid,task, sequenceno, cast('/1/'+sequenceno+ cast(trank as varchar)+'/' as hierarchyid)rid into #tempprecedence5_Hierarchy FROM #tempprecedence5
SELECT rowid
,task
,row_number() OVER (
PARTITION BY rowid ORDER BY rid
) sequenceno
INTO #tempprecedence6
FROM #tempprecedence5_Hierarchy;
CREATE CLUSTERED INDEX CIX_TblPrecedenceConstraint1 ON #tempprecedence6 (RowID);
CREATE NONCLUSTERED INDEX NCIX_TblPrecedenceConstraint1 ON #tempprecedence6 (task);
/*
select t1.task,t1.parentlevel,t.task as t1,row_number() over(partition by t1.rowid order by t.level) level,t.rowid into #tempprecedence3
from #tempprecedence2 t
join #tempprecedence2 t1
on charindex(t.task, t1.taskFrom)>0
and t1.parentlevel >1 and t.parentlevel =1
and t.rowid =t1.rowid
order by t.rowid, t1.parentlevel ,t.level
select rowid, task,row_number() over(partition by rowid order by max(sequenceno)) sequenceno into #tempprecedence4
from(
select distinct t1.rowid,isnull(t2.task,t1.task) Task,row_number() over(partition by t1.rowid order by t1.parentlevel,t1.level,isnull(t2.level,1)) sequenceno
from #tempprecedence2 t1
left join #tempprecedence3 t2
on t1.rowid = t2.rowid
and t1.task = t2.t1
where t1.parentlevel =1 )t
group by rowid,task
*/
PRINT '----------------Get precedence constraint details------------------------------';
PRINT '----------------Get all Control flow tasks details------------------------------';
PRINT '----------------Get all data flow tasks details------------------------------';
WITH CTE_CFLevel
AS (
SELECT pkg.RowID
,cfnodes.x.query('.').query('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
./DTS:Executable[@DTS:ExecutableType=''SSIS.Pipeline.3'']/DTS:ObjectData/pipeline/components/component') DFTQuery
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:refId[1]', 'varchar(max)') AS TaskPath
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:ObjectName[1]', 'nvarchar(max)') AS TaskName
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:Description', 'nvarchar(max)') AS TaskTypeDescription
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:ExecutableType', 'nvarchar(max)') AS TaskType
,cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:DelayValidation', 'nvarchar(max)') AS DelayValidationPropertyValue
,ISNULL(cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:Disabled', 'nvarchar(max)'), 'False') AS IsDisabled
,'EvalExpression = ' + cfnodes.x.value('declare namespace p1="www.microsoft.com/SqlServer/Dts";./@p1:EvalExpression', 'nvarchar(max)') AS ForloopEvalExpression
,cfnodes.x.query('.').query('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
./DTS:Executable[@DTS:ExecutableType=''Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'']/DTS:ObjectData/*') AS SqlTaskQry
,cfnodes.x.query('.').query('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
./DTS:Executable[@DTS:ExecutableType=''SSIS.ExecutePackageTask.3'']/*') ExecPkgTaskQry
,cfnodes.x.query('.').query('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
./DTS:Executable[@DTS:ExecutableType=''Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptTask, Microsoft.SqlServer.ScriptTask, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'']/DTS:ObjectData/ScriptProject/*') ScriptTaskQry
FROM pkgStats pkg
CROSS APPLY pkg.PackageXML.nodes('declare namespace DTS="www.microsoft.com/SqlServer/Dts";//DTS:Executable/DTS:Executables/*') AS cfnodes(x)
)
INSERT INTO TblControlFlowDetails (
RowID
,TaskPath
,TaskName
,TaskTypeDescription
,TaskType
,DelayValidationPropertyValue
,DFTQuery
,SqlTaskQry
,ExecPkgTaskQry
,ScriptTaskQry
,IsDisabled
,ExpressionValue
)
SELECT RowID
,TaskPath
,TaskName
,TaskTypeDescription
,TaskType
,DelayValidationPropertyValue
,DFTQuery
,SqlTaskQry
,ExecPkgTaskQry
,ScriptTaskQry
,IsDisabled
,ForloopEvalExpression
FROM CTE_CFLevel;
PRINT '---------- Insert Script task details-----------------------';
INSERT INTO TblScriptTaskdetails (
RowID
,ControlFlowDetailsRowID
,Script
)
SELECT RowID
,CF.ControlFlowDetailsRowID
,cfnodes1.x.value('./ProjectItem[@Name=''ScriptMain.cs''][1]', 'varchar(max)') Script
FROM TblControlFlowDetails cf
CROSS APPLY Cf.ScriptTaskQry.nodes('.') AS cfnodes1(x);
PRINT '---------- Insert Execute package task details-----------------------';
INSERT INTO TblExecutePackageTaskDetails (
RowID
,ControlFlowDetailsRowID
,ExecutePackageExpression
,ExecutedPackageName
,ExecutePackageConnection
)
SELECT RowID
,cf.ControlFlowDetailsRowID
,cfnodes1.x.value('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
./DTS:PropertyExpression[1]', 'varchar(1000)') ExecutePackageExpression
,cfnodes1.x.value('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
(./DTS:ObjectData/ExecutePackageTask/PackageName)[1]', 'varchar(1000)') ExecutedPackageName
,cfnodes1.x.value('declare namespace DTS="www.microsoft.com/SqlServer/Dts";
(./DTS:ObjectData/ExecutePackageTask/Connection)[1]', 'varchar(1000)') ExecutePackageConnection
FROM TblControlFlowDetails cf
CROSS APPLY Cf.ExecPkgTaskQry.nodes('.') AS cfnodes1(x);
UPDATE execpkg
SET ExecutePackageConnection = pkg.ObjectValue
FROM TblExecutePackageTaskDetails execpkg
INNER JOIN packageanalysis pkg ON execpkg.RowID = pkg.RowID
AND pkg.category = 'Connection'
AND pkg.ConnectionManagerID = execpkg.ExecutePackageConnection;
PRINT '---------- Insert Execute SQL task details-----------------------';
INSERT INTO TblSQLTaskDetails (
RowID
,ControlFlowDetailsRowID
,ParameterBindingSequence
,ParameterBindingParameterName
)
SELECT DISTINCT cf.RowID
,cf.ControlFlowDetailsRowID
--,cfnodes1.x.value('declare namespace SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask"; (./@SQLTask:DtsVariableName)','varchar(1000)') as ResultSetParameterName
,cfnodes.x.value('declare namespace SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask"; (./@SQLTask:ParameterName)', 'varchar(1000)') AS ParameterBindingSequence
,cfnodes.x.value('declare namespace SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask"; (./@SQLTask:DtsVariableName)', 'varchar(1000)') AS ParameterBindingParameterName
FROM TblControlFlowDetails cf
OUTER APPLY Cf.SqlTaskQry.nodes('declare namespace SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask";./SQLTask:SqlTaskData/SQLTask:ParameterBinding') AS cfnodes(x)
ORDER BY 1
,2
,3;
UPDATE sqltsk
SET ResultSetParameterName = CF.ResultSetParameterName
FROM TblSQLTaskDetails sqltsk
INNER JOIN (
SELECT DISTINCT cf.RowID
,cf.ControlFlowDetailsRowID
,cast(ROW_NUMBER() OVER (
PARTITION BY cf.ControlFlowDetailsRowID ORDER BY (
SELECT 1
)
) - 1 AS VARCHAR) rid
,cfnodes1.x.value('declare namespace SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask"; (./@SQLTask:DtsVariableName)', 'varchar(1000)') AS ResultSetParameterName
FROM TblControlFlowDetails cf
OUTER APPLY Cf.SqlTaskQry.nodes('declare namespace SQLTask="www.microsoft.com/sqlserver/dts/tasks/sqltask";./SQLTask:SqlTaskData/SQLTask:ResultBinding') AS cfnodes1(x)
) CF ON sqltsk.ControlFlowDetailsRowID = CF.ControlFlowDetailsRowID
AND cf.rid = ISNULL(sqltsk.ParameterBindingSequence, 0)
PRINT '---------- Insert DFT details-----------------------';
INSERT INTO TblDFTTaskDetails (
RowID
,DFTTasksPath
,DFTTaskName
,DFTTaskType
,DFTTaskType1
,DFTRowSet
,ParameterBindingParameterName
,DFTSQLCommand
,DFTConnectionManager
,Variable
,IsSortedProperty
,InputQry
,OutputQry
,MultihashcolumnSortPosition
)
SELECT RowID
,dftnodes.x.value('@refId[1]', 'varchar(max)') AS DFTTasksPath
,dftnodes.x.value('@name[1]', 'varchar(max)') AS DFTTaskName
,dftnodes.x.value('@description[1]', 'varchar(max)') AS DFTTaskType
,dftnodes.x.value('@contactInfo[1]', 'varchar(max)') AS DFTTaskType1
,dftnodes.x.value('data(./properties/property[@name=''OpenRowset''] )[1]', 'varchar(max)') DFTRowSet
,dftnodes.x.value('data(./properties/property[@name=''ParameterMapping''] )[1]', 'varchar(max)') ParameterBindingParameterName
,CASE
WHEN isnull(dftnodes.x.value('data(./properties/property[@name=''SqlCommand''])[1]', 'varchar(max)'), '') = ''
THEN isnull(dftnodes.x.value('data(./properties/property[@name=''SqlCommandVariable''])[1]', 'varchar(max)'), '')
ELSE isnull(dftnodes.x.value('data(./properties/property[@name=''SqlCommand''])[1]', 'varchar(max)'), '')
END DFTSQLCommand
,dftnodes.x.value('data(./connections/connection/@connectionManagerID)[1]', 'varchar(max)') DFTConnectionManager
,dftnodes.x.value('data(./properties/property[@name=''VariableName''] )[1]', 'varchar(max)') Variable
,dftnodes.x.value('data(./outputs/output/@isSorted)[1]', 'varchar(10)') IsSortedProperty
,lineage.x.query('.') InputQry
,outputvalue.x.query('.') OutputQry
,outputvalue.x.value('data(./properties/property[@name=''InputColumnLineageIDs''] )[1]', 'varchar(max)') as MultihashcolumnSortPosition