-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTX-Configuration-Deployment-Script.sql
3305 lines (2586 loc) · 116 KB
/
CTX-Configuration-Deployment-Script.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
/*
Deployment script for Cortex-ConfigStore
This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
:setvar CortexDBUser "domain\CTX_App_User_Service_Account"
:setvar DatabaseFilePath "C:\Cortex Databases"
:setvar DatabaseLogPath "C:\Cortex Databases"
:setvar Distribution_DataPath "C:\Cortex Databases\Distribution"
:setvar Distribution_LogPath "C:\Cortex Databases\Distribution"
:setvar InstanceName "LIVE_DBServerName\LIVE_InstanceName"
:setvar isReplicated "true"
:setvar MachineName "LIVE_DBServerName"
:setvar REPL_Admin_User "domain\CTX_SQL_Admin_User"
:setvar REPL_Working_Directory "C:\Cortex Databases\Replication Data"
:setvar ResilientInstanceName "DR_DBServerName\DR_InstanceName"
:setvar DatabaseName "Cortex-ConfigStore"
:setvar DefaultFilePrefix "Cortex-ConfigStore"
GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF;
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
GO
USE [master];
GO
IF (DB_ID(N'$(DatabaseName)') IS NOT NULL)
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [$(DatabaseName)];
END
GO
PRINT N'Creating database $(DatabaseName)...'
GO
CREATE DATABASE [$(DatabaseName)]
ON
PRIMARY(NAME = [Cortex_ConfigStore_Data], FILENAME = '$(DatabaseFilePath)\$(DatabaseName)\Datafile\$(DatabaseName).mdf')
LOG ON (NAME = [Cortex_ConfigStore_log], FILENAME = '$(DatabaseLogPath)\$(DatabaseName)\Logfile\$(DatabaseName).ldf') COLLATE SQL_Latin1_General_CP1_CI_AS
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET AUTO_CLOSE OFF
WITH ROLLBACK IMMEDIATE;
END
GO
USE [$(DatabaseName)];
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET ANSI_NULLS ON,
ANSI_PADDING ON,
ANSI_WARNINGS ON,
ARITHABORT ON,
CONCAT_NULL_YIELDS_NULL ON,
NUMERIC_ROUNDABORT OFF,
QUOTED_IDENTIFIER ON,
ANSI_NULL_DEFAULT ON,
CURSOR_DEFAULT LOCAL,
RECOVERY FULL,
CURSOR_CLOSE_ON_COMMIT OFF,
AUTO_CREATE_STATISTICS ON,
AUTO_SHRINK OFF,
AUTO_UPDATE_STATISTICS ON,
RECURSIVE_TRIGGERS OFF
WITH ROLLBACK IMMEDIATE;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET ALLOW_SNAPSHOT_ISOLATION OFF;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET READ_COMMITTED_SNAPSHOT OFF
WITH ROLLBACK IMMEDIATE;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET AUTO_UPDATE_STATISTICS_ASYNC OFF,
PAGE_VERIFY NONE,
DATE_CORRELATION_OPTIMIZATION OFF,
DISABLE_BROKER,
PARAMETERIZATION SIMPLE,
SUPPLEMENTAL_LOGGING OFF
WITH ROLLBACK IMMEDIATE;
END
GO
IF IS_SRVROLEMEMBER(N'sysadmin') = 1
BEGIN
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
SET TRUSTWORTHY OFF,
DB_CHAINING OFF
WITH ROLLBACK IMMEDIATE';
END
END
ELSE
BEGIN
PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
END
GO
IF IS_SRVROLEMEMBER(N'sysadmin') = 1
BEGIN
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
EXECUTE sp_executesql N'ALTER DATABASE [$(DatabaseName)]
SET HONOR_BROKER_PRIORITY OFF
WITH ROLLBACK IMMEDIATE';
END
END
ELSE
BEGIN
PRINT N'The database settings cannot be modified. You must be a SysAdmin to apply these settings.';
END
GO
ALTER DATABASE [$(DatabaseName)]
SET TARGET_RECOVERY_TIME = 0 SECONDS
WITH ROLLBACK IMMEDIATE;
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET FILESTREAM(NON_TRANSACTED_ACCESS = OFF),
CONTAINMENT = NONE
WITH ROLLBACK IMMEDIATE;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET AUTO_CREATE_STATISTICS ON(INCREMENTAL = OFF),
MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT = OFF,
DELAYED_DURABILITY = DISABLED
WITH ROLLBACK IMMEDIATE;
END
GO
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
END
GO
IF fulltextserviceproperty(N'IsFulltextInstalled') = 1
EXECUTE sp_fulltext_database 'enable';
GO
/*
Pre-Deployment Script Template
--------------------------------------------------------------------------------------
This file contains SQL statements that will be executed before the build script.
Use SQLCMD syntax to include a file in the pre-deployment script.
Example: :r .\myfile.sql
Use SQLCMD syntax to reference a variable in the pre-deployment script.
Example: :setvar TableName MyTable
SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/
USE [$(DatabaseName)];
--Create logins if they don't exist
IF NOT EXISTS (SELECT * FROM sys.server_principals WHERE name = N'$(CortexDBUser)')
BEGIN TRY
CREATE LOGIN [$(CortexDBUser)] FROM WINDOWS WITH DEFAULT_DATABASE=[master]
PRINT 'Created login $(CortexDBUser)'
END TRY
BEGIN CATCH
PRINT '$(CortexDBUser) login does not exist'
END CATCH
IF ('$(isReplicated)' = 'true')
BEGIN
IF NOT EXISTS (SELECT srv.name
FROM sys.servers srv
WHERE srv.server_id != 0
AND srv.name = N'REPL-$(ResilientInstanceName)')
BEGIN
EXEC master.dbo.sp_addlinkedserver @server = N'REPL-$(ResilientInstanceName)', @srvproduct='', @provider='SQLOLEDB', @datasrc=N'$(ResilientInstanceName)'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'REPL-$(ResilientInstanceName)',@useself=N'True',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'collation compatible', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'data access', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'dist', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'pub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'rpc', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'rpc out', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'sub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'connect timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'collation name', @optvalue=null
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'lazy schema validation', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'query timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'use remote collation', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'REPL-$(ResilientInstanceName)', @optname=N'remote proc transaction promotion', @optvalue=N'true'
END
END
GO
GO
PRINT N'Creating Table [dbo].[ADGroup]...';
GO
CREATE TABLE [dbo].[ADGroup] (
[ID] INT NOT NULL,
[GroupName] VARCHAR (50) NULL,
[Description] VARCHAR (500) NULL,
CONSTRAINT [PK_ADGroup] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [unq_ADGroupConstraint] UNIQUE NONCLUSTERED ([GroupName] ASC)
);
GO
PRINT N'Creating Table [dbo].[AreaADGroupsLink]...';
GO
CREATE TABLE [dbo].[AreaADGroupsLink] (
[ID] INT NOT NULL,
[AreaID] INT NOT NULL,
[ADGroupID] INT NOT NULL,
CONSTRAINT [PK_AreaADGroupsLink] PRIMARY KEY CLUSTERED ([ID] ASC)
);
GO
PRINT N'Creating Table [dbo].[Areas]...';
GO
CREATE TABLE [dbo].[Areas] (
[ID] INT NOT NULL,
[Area] VARCHAR (50) NOT NULL,
[Description] VARCHAR (50) NULL,
CONSTRAINT [PK_Areas-v2] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY],
CONSTRAINT [Area_Unique] UNIQUE NONCLUSTERED ([Area] ASC) ON [PRIMARY]
) ON [PRIMARY];
GO
PRINT N'Creating Table [dbo].[Customers]...';
GO
CREATE TABLE [dbo].[Customers] (
[ID] INT NOT NULL,
[Customer] VARCHAR (50) NOT NULL,
[Description] VARCHAR (50) NULL,
CONSTRAINT [PK_Customers-v2] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY],
CONSTRAINT [Cust_Unique] UNIQUE NONCLUSTERED ([Customer] ASC) ON [PRIMARY]
) ON [PRIMARY];
GO
PRINT N'Creating Table [dbo].[Environments]...';
GO
CREATE TABLE [dbo].[Environments] (
[ID] INT NOT NULL,
[Environment] VARCHAR (50) NOT NULL,
[Description] VARCHAR (50) NULL,
CONSTRAINT [PK_Environments-v2] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY],
CONSTRAINT [Env_Unique] UNIQUE NONCLUSTERED ([Environment] ASC) ON [PRIMARY]
) ON [PRIMARY];
GO
PRINT N'Creating Table [dbo].[Param_Name]...';
GO
CREATE TABLE [dbo].[Param_Name] (
[ID] INT NOT NULL,
[Param_Name] VARCHAR (50) NOT NULL,
[Description] VARCHAR (500) NULL,
CONSTRAINT [PK_Param_Name-v2] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY],
CONSTRAINT [Prm_Unique] UNIQUE NONCLUSTERED ([Param_Name] ASC) ON [PRIMARY]
) ON [PRIMARY];
GO
PRINT N'Creating Table [dbo].[Param_Values]...';
GO
CREATE TABLE [dbo].[Param_Values] (
[ID] INT NOT NULL,
[AreaID] INT NOT NULL,
[CustomerID] INT NULL,
[EnvironmentID] INT NULL,
[Param_NameID] INT NOT NULL,
[Param_Value] VARCHAR (MAX) NOT NULL,
[Description] VARCHAR (500) NULL,
CONSTRAINT [PK_Param_Values-v2] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY],
CONSTRAINT [unique_prm] UNIQUE NONCLUSTERED ([AreaID] ASC, [CustomerID] ASC, [EnvironmentID] ASC, [Param_NameID] ASC) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
GO
PRINT N'Creating Table [dbo].[SYS_Sequence]...';
GO
CREATE TABLE [dbo].[SYS_Sequence] (
[TableName] VARCHAR (100) NOT NULL,
[ColumnName] VARCHAR (100) NOT NULL,
[SequenceNumber] BIGINT NOT NULL,
CONSTRAINT [PK_SYS_Sequence] PRIMARY KEY CLUSTERED ([TableName] ASC)
);
GO
PRINT N'Creating Table [dbo].[SYS_SETTINGS]...';
GO
CREATE TABLE [dbo].[SYS_SETTINGS] (
[KEY] VARCHAR (32) NOT NULL,
[VALUE] VARCHAR (MAX) NOT NULL,
[DESCRIPTION] VARCHAR (MAX) NULL,
CONSTRAINT [PK_SYS_SETTINGS] PRIMARY KEY CLUSTERED ([KEY] ASC)
);
GO
PRINT N'Creating Foreign Key [dbo].[FK_Param_Values_Areas]...';
GO
ALTER TABLE [dbo].[Param_Values]
ADD CONSTRAINT [FK_Param_Values_Areas] FOREIGN KEY ([AreaID]) REFERENCES [dbo].[Areas] ([ID]);
GO
PRINT N'Creating Foreign Key [dbo].[FK_Param_Values_Customers]...';
GO
ALTER TABLE [dbo].[Param_Values]
ADD CONSTRAINT [FK_Param_Values_Customers] FOREIGN KEY ([CustomerID]) REFERENCES [dbo].[Customers] ([ID]);
GO
PRINT N'Creating Foreign Key [dbo].[FK_Param_Values_Environments]...';
GO
ALTER TABLE [dbo].[Param_Values]
ADD CONSTRAINT [FK_Param_Values_Environments] FOREIGN KEY ([EnvironmentID]) REFERENCES [dbo].[Environments] ([ID]);
GO
PRINT N'Creating Foreign Key [dbo].[FK_Param_Values_Param_Name]...';
GO
ALTER TABLE [dbo].[Param_Values]
ADD CONSTRAINT [FK_Param_Values_Param_Name] FOREIGN KEY ([Param_NameID]) REFERENCES [dbo].[Param_Name] ([ID]);
GO
PRINT N'Creating View [dbo].[CFG_View]...';
GO
CREATE VIEW [dbo].[CFG_View]
AS
SELECT TOP 100 PERCENT A.Area,
C.Customer,
E.Environment,
PN.Param_name,
PV.Description,
PV.Param_value
FROM dbo.param_values AS PV
INNER JOIN dbo.param_name AS PN
ON PV.param_nameid = PN.id
INNER JOIN dbo.areas AS A
ON PV.areaid = A.id
LEFT OUTER JOIN dbo.environments AS E
ON PV.environmentid = E.id
LEFT OUTER JOIN dbo.customers AS C
ON PV.customerid = C.id
ORDER BY A.Area,
C.Customer,
E.Environment,
PN.Param_name
GO
PRINT N'Creating View [dbo].[CGF_All_Permutations]...';
GO
CREATE VIEW [dbo].[CGF_All_Permutations] AS
SELECT T.Area
, T.Customer
, T.Environment
, T.Param_Name
, PV.ID
, PV.Param_Value
, PV.Description
FROM
(SELECT A.ID AS AreaID
, A.Area
, C.ID AS CustomerID
, C.Customer
, E.ID AS EnvironmentID
, E.Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A
CROSS JOIN Customers C
CROSS JOIN Environments E
UNION
SELECT A.ID AS AreaID
, A.Area
, C.ID AS CustomerID
, C.Customer
, NULL AS EnvironmentID
, NULL Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A
CROSS JOIN Customers C
UNION
SELECT A.ID AS AreaID
, A.Area
, NULL AS CustomerID
, NULL AS Customer
, E.ID AS EnvironmentID
, E.Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A
CROSS JOIN Environments E
UNION
SELECT A.ID AS AreaID
, A.Area
, NULL AS CustomerID
, NULL AS Customer
, NULL AS EnvironmentID
, NULL AS Environment
, PN.ID AS ParamID
, PN.Param_Name
FROM Param_Name PN
CROSS JOIN Areas A) T
LEFT OUTER JOIN Param_Values PV
ON ISNULL(PV.Param_NameID,-1) = ISNULL(T.ParamID,-1)
AND ISNULL(PV.AreaID,-1) = ISNULL(T.AreaID,-1)
AND ISNULL(PV.CustomerID,-1) = ISNULL(T.CustomerID,-1)
AND ISNULL(PV.EnvironmentID,-1) = ISNULL(T.EnvironmentID,-1)
GO
PRINT N'Creating Function [dbo].[udf_Get_Parameter_Value]...';
GO
CREATE FUNCTION [dbo].[udf_Get_Parameter_Value](@pi_Param_Name varchar(200))
RETURNS varchar(200) AS
BEGIN
declare @return_value varchar(200)
select @return_value = [VALUE]
from SYS_SETTINGS
where [KEY] = @pi_Param_Name
return @return_value
END
GO
PRINT N'Creating Function [dbo].[udf_GetSequenceNumber]...';
GO
CREATE FUNCTION [dbo].[udf_GetSequenceNumber]
(
@i_sTableName VARCHAR(100)
, @i_sColumnName VARCHAR(100)
)
RETURNS BIGINT
AS
BEGIN
DECLARE @nSequenceNumber BIGINT
SELECT @nSequenceNumber = [SequenceNumber]
FROM SYS_Sequence
WHERE [TableName] = @i_sTableName
AND [ColumnName] = @i_sColumnName
RETURN @nSequenceNumber
END
GO
PRINT N'Creating Trigger [dbo].[TRG_InsteadOfInsert_ADGroup]...';
GO
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_ADGroup]
ON [dbo].[ADGroup]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ADGroupID int = (SELECT dbo.udf_GetSequenceNumber('ADGroup','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO ADGroup (ID
,GroupName)
SELECT @ADGroupID
,inserted.GroupName
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @ADGroupID + 1
WHERE TableName = 'ADGroup'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
PRINT N'Creating Trigger [dbo].[TRG_InsteadOfInsert_AreaADGroupsLink]...';
GO
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_AreaADGroupsLink]
ON [dbo].[AreaADGroupsLink]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @AreaADGroupsLinkID int = (SELECT dbo.udf_GetSequenceNumber('AreaADGroupsLink','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO AreaADGroupsLink (ID
,AreaID
,ADGroupID)
SELECT @AreaADGroupsLinkID
,inserted.AreaID,inserted.ADGroupID
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @AreaADGroupsLinkID + 1
WHERE TableName = 'AreaADGroupsLink'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
PRINT N'Creating Trigger [dbo].[TRG_InsteadOfInsert_Areas]...';
GO
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_Areas]
ON [dbo].[Areas]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @AreaID int = (SELECT dbo.udf_GetSequenceNumber('Areas','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Areas (ID
,Area
,Description)
SELECT @AreaID
,inserted.Area
,inserted.Description
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @AreaID + 1
WHERE TableName = 'Areas'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
PRINT N'Creating Trigger [dbo].[TRG_InsteadOfInsert_Customers]...';
GO
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_Customers]
ON [dbo].[Customers]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @CustID int = (SELECT dbo.udf_GetSequenceNumber('Customers','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Customers (ID
,Customer
,Description)
SELECT @CustID
,inserted.Customer
,inserted.Description
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @CustID + 1
WHERE TableName = 'Customers'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
PRINT N'Creating Trigger [dbo].[TRG_InsteadOfInsert_Environments]...';
GO
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_Environments]
ON [dbo].[Environments]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @EnvID int = (SELECT dbo.udf_GetSequenceNumber('Environments','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Environments (ID
,Environment
,Description)
SELECT @EnvID
,inserted.Environment
,inserted.Description
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @EnvID + 1
WHERE TableName = 'Environments'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
PRINT N'Creating Trigger [dbo].[TRG_InsteadOfInsert_Param_Name]...';
GO
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_Param_Name]
ON [dbo].[Param_Name]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @PrmID int = (SELECT dbo.udf_GetSequenceNumber('Param_Name','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Param_Name (ID
,Param_Name
,Description)
SELECT @PrmID
,inserted.Param_Name
,inserted.Description
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @PrmID + 1
WHERE TableName = 'Param_Name'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
PRINT N'Creating Trigger [dbo].[TRG_InsteadOfInsert_Param_Values]...';
GO
CREATE TRIGGER [dbo].[TRG_InsteadOfInsert_Param_Values]
ON [dbo].[Param_Values]
INSTEAD OF INSERT NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON;
DECLARE @CfgValID int = (SELECT dbo.udf_GetSequenceNumber('Param_Values','ID'));
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Param_Values (ID
,AreaID
,CustomerID
,EnvironmentID
,Param_NameID
,Param_Value
,Description)
SELECT @CfgValID
,inserted.AreaID
,inserted.CustomerID
,inserted.EnvironmentID
,inserted.Param_NameID
,inserted.Param_Value
,inserted.Description
FROM inserted
UPDATE SYS_Sequence
SET SequenceNumber = @CfgValID + 1
WHERE TableName = 'Param_Values'
AND ColumnName = 'ID'
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
END CATCH
END
GO
PRINT N'Creating Function [dbo].[udf_split_string]...';
GO
-- =============================================
-- Author: Ravi
-- Create date: 28-10-2021
-- Description: comma separated values to list
-- =============================================
CREATE FUNCTION [dbo].[udf_split_string]
(
@in_string VARCHAR(MAX),
@delimiter VARCHAR(1)
)
RETURNS @list TABLE(tuple VARCHAR(100))
AS
BEGIN
WHILE LEN(@in_string) > 0
BEGIN
INSERT INTO @list(tuple)
SELECT left(@in_string, charindex(@delimiter, @in_string+',') -1) as tuple
SET @in_string = stuff(@in_string, 1, charindex(@delimiter, @in_string + @delimiter), '')
end
RETURN
END