	--Configuration Tables - add Affiliated parameters:
	--01
	--Affiliated ENTITY:
	DECLARE @ConfigurationEntityTypes TABLE ([Id] [int] NOT NULL,[Title] [nvarchar](max) NOT NULL);

	INSERT into @ConfigurationEntityTypes ([Id], [Title]) VALUES (14, N'Affiliated');

	--select * from  dbo.[ConfigurationEntityTypes]
	SET IDENTITY_INSERT [dbo].[ConfigurationEntityTypes] ON;

		MERGE INTO dbo.[ConfigurationEntityTypes] AS TARGET  
			USING @ConfigurationEntityTypes AS SOURCE  
			ON TARGET.Id = SOURCE.Id	
			WHEN MATCHED THEN    
				UPDATE SET Title = SOURCE.Title
			WHEN NOT MATCHED BY TARGET THEN   
				INSERT ([Id], [Title]) VALUES (SOURCE.Id, SOURCE.Title)  
		OUTPUT $action, SOURCE.*;
		
	SET IDENTITY_INSERT [dbo].[ConfigurationEntityTypes] OFF;

	---------------------------------------------------------------
	---------------------------------------------------------------
	--02 Parameters for Affiliated
	DECLARE @ConfigurationParameterTypes TABLE (Id int NOT NULL,Title nvarchar(max) NOT NULL);
	
	--Affiliated
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (72, N'LoginUserName')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (73, N'LoginPassword')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (74, N'Timeout')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (75, N'Url')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (76, N'ClientId')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (77, N'ContentType')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (78, N'Method')
	--INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (35, N'SslCheck')
	
	SET IDENTITY_INSERT dbo.ConfigurationParameterTypes ON

	MERGE INTO dbo.ConfigurationParameterTypes AS TARGET  
		USING @ConfigurationParameterTypes AS SOURCE  
		ON TARGET.Id = SOURCE.Id
		WHEN MATCHED THEN    
			UPDATE SET Title = SOURCE.Title
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (Id, Title) VALUES (SOURCE.Id, SOURCE.Title)  
	OUTPUT $action, SOURCE.*;

	SET IDENTITY_INSERT dbo.ConfigurationParameterTypes OFF
    
	---------------------------------------------------------------
	---------------------------------------------------------------
	--03 define parameters for entity Affiliated (14)
	
	DECLARE @ConfigurationEntityParameters TABLE (Id int NOT NULL,EntityId int NOT NULL,ParameterId int not null);

	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 96,14, 73) --Password
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 97,14, 74)   --Timeout
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 98,14, 75)	--Url
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 99,14, 72)	--UserName
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 100,14,76)	--ClientId
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 101,14,77)	--ContentType
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 102,14,78)	--Method
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 103,14,35)	--SslCheck
							 		
	MERGE INTO dbo.ConfigurationEntityParameters AS TARGET  							 
		USING @ConfigurationEntityParameters AS SOURCE  								 
		ON TARGET.Id = SOURCE.Id
		WHEN MATCHED THEN    
			UPDATE SET entityid = SOURCE.entityid,ParameterId=source.ParameterId 
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (Id, EntityId,ParameterId) VALUES (SOURCE.Id,Source.EntityId, SOURCE.ParameterId)  
	OUTPUT $action, SOURCE.*;

	---------------------------------------------------------------
	---------------------------------------------------------------
	--04 - create 2 groups for Affiliated Entity
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );
	
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, 'b696e645-a597-4990-a996-99289ad86387') --Affiliated
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, 'A58FE193-433E-45A6-8B6C-BE69431ED1E3') --Affiliated_HB

	MERGE INTO dbo.ConfigurationGroups AS TARGET  
		USING @ConfigurationGroups AS SOURCE  
		ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
		WHEN MATCHED THEN    
				UPDATE SET entityid = SOURCE.entityid
		WHEN NOT MATCHED BY TARGET THEN   
				INSERT (EntityId,GuidKey) VALUES (Source.EntityId,upper(source.GuidKey)) 
	OUTPUT $action, SOURCE.*;
	

	---------------------------------------------------------------
	---------------------------------------------------------------
	--04 - create 2 groups for Affiliated Entity		
								
	declare @configurationGroupIdAffiliated int
	select @configurationGroupIdAffiliated = id from ConfigurationGroups where guidkey='b696e645-a597-4990-a996-99289ad86387'

	declare @configurationGroupIdAffiliated_HB int
	select @configurationGroupIdAffiliated_HB = id from ConfigurationGroups where guidkey='A58FE193-433E-45A6-8B6C-BE69431ED1E3'
		
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));
	
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,72,  N'Essenceapi'	   						-- N'UserName')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,73,  N'484*fD@wrTT'							-- N'Password')	
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,74,  N'60000'								-- N'Timeout')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,75,  N'https://alarm.alertmessage.com/'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,76,  N'affiliated-alarm'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,78,  N'POST'								-- N'Method')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,35,  N'False'								-- N'SslCheck')
	
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,72,  N'Essenceapi'							-- N'HB_UserName')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,73,  N'484*fD@wrTT'							-- N'HB_Password')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,74,  N'6000'								-- N'HB_Timeout')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,75,  N'https://alarm.alertmessage.com/'		-- N'HB_Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,76,  N'affiliated-alarm'					-- N'HB_ClientId')
    INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,77,  N'3'									-- N'HB_ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,78,  N'POST'								-- N'HB_Method')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,35,  N'False'						    	-- N'HB_SslCheck')

	MERGE INTO dbo.ConfigurationGroupParameters AS TARGET  
			USING @ConfigurationGroupParameters AS SOURCE  
			ON TARGET.GroupId  = SOURCE.GroupId
			AND TARGET.ParameterId =  SOURCE.ParameterId
			WHEN MATCHED THEN    
					UPDATE SET Value=source.Value
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( GroupId,ParameterId,Value) VALUES (Source.GroupId, SOURCE.ParameterId,source.Value)  
	OUTPUT $action, SOURCE.*;

	---------------------------------------------------------------
	---------------------------------------------------------------
	--06: ConfigurationEntityTree
	--Set the identifier in the tree for the instance
	--declare @configurationGroupIdAffiliated int
	select @configurationGroupIdAffiliated = id from ConfigurationGroups where guidkey='b696e645-a597-4990-a996-99289ad86387'

	DECLARE @ConfigurationEntityTree TABLE (EntityTypeId int not null,GroupId int NOT NULL, MachineName nvarchar(max), ServiceName nvarchar(max), ServiceId nvarchar(max), 
											ModuleIdentifier nvarchar(max),ParentEntityId int null,GuidKey uniqueidentifier not null );


	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliated, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'Affiliated', NULL, 'A4E36C1F-6B1F-4029-BE7C-7AE97F95DDF1'
	
	
	MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree AS SOURCE  
			ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
			WHEN MATCHED THEN    
					UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
					ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
					VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
	OUTPUT $action, SOURCE.*;

	---------------------------------------------------------------
	---------------------------------------------------------------
	--07: ConfigurationEntityTree
	--after having parent id insert Affiliated_HB

	declare @configurationGroupIdAffiliatedHB int
	select @configurationGroupIdAffiliatedHB = id from ConfigurationGroups where guidkey='A58FE193-433E-45A6-8B6C-BE69431ED1E3'

	DECLARE @ConfigurationEntityTree2 TABLE (EntityTypeId int not null,GroupId int NOT NULL, MachineName nvarchar(max), ServiceName nvarchar(max), ServiceId nvarchar(max), 
											ModuleIdentifier nvarchar(max),ParentEntityId int null,GuidKey uniqueidentifier not null );


	INSERT into @ConfigurationEntityTree2 ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliatedHB, N'MachineId', N'Affiliated', N'AffiliatedHB',N'Affiliated',t.id, 'FA762A1D-678B-4421-84C1-6DB86814FAE4'
	from ConfigurationEntityTree t where t.guidkey='A4E36C1F-6B1F-4029-BE7C-7AE97F95DDF1' --AffiliatedDefault tree row
	
	
	MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree2 AS SOURCE  
			ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
			WHEN MATCHED THEN    
					UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
					ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
					VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
	OUTPUT $action, SOURCE.*;

	select * from ConfigurationEntityTree