
	--01
	DECLARE @EventGroups TABLE 
		( 
			EventGroupTypeId INT NOT NULL, 
			EventId INT NOT NULL
		);

	DECLARE @EventGroupTypeIdSafetySecurity INT
	SELECT @EventGroupTypeIdSafetySecurity  = EGT.Id
	FROM EventGroupType	 EGT 
	WHERE EGT.EventGroupTypeType = 'Safety And Security'

	DECLARE @EventGroupTypeIdSystem INT
	SELECT @EventGroupTypeIdSystem   = EGT.Id
	FROM EventGroupType	 EGT 
	WHERE EGT.EventGroupTypeType = 'System'
	
	DECLARE @EventGroupTypeIdComfort INT
	SELECT @EventGroupTypeIdComfort   = EGT.Id
	FROM EventGroupType	 EGT 
	WHERE EGT.EventGroupTypeType = 'Comfort' AND EGT.ProductFamily = 1
			
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 3))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 1))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 5))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 153))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 1253))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 152))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 151))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 44))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 706))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 155))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 104))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 106))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 105))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 101))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 103))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 102))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 150))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 107))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSafetySecurity,(SELECT Id FROM CD_Event WHERE Event_Code = 109))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 211))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 204))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 203))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 9))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 1010))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 314))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 201))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 202))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 705))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdSystem,(SELECT Id FROM CD_Event WHERE Event_Code = 901))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdComfort,(SELECT Id FROM CD_Event WHERE Event_Code = 1256))
	INSERT @EventGroups ( EventGroupTypeId, EventId ) VALUES (@EventGroupTypeIdComfort,(SELECT Id FROM CD_Event WHERE Event_Code = 1257))
		
	MERGE INTO dbo.EventGroups AS TARGET
	USING @EventGroups AS SOURCE
	ON SOURCE.EventGroupTypeId = TARGET.EventGroupTypeId 
		AND SOURCE.EventId = TARGET.EventId 
	WHEN MATCHED THEN    
	UPDATE SET EventGroupTypeId = SOURCE.EventGroupTypeId,
			   EventId = SOURCE.EventId
	WHEN NOT MATCHED BY TARGET THEN
			INSERT ( EventGroupTypeId, EventId)
			VALUES (SOURCE.EventGroupTypeId,SOURCE.EventId )
	OUTPUT $ACTION, SOURCE.*;
	
	
	--02
	DECLARE @ServicePackages TABLE ([InternalId] [int] NOT NULL,[Id] [int] NOT NULL,[Name] [nvarchar](200) NOT NULL,[Description] [nvarchar](400) NOT NULL,[PanelGroupId] [int] NOT NULL,[PackageLevel] [int] NOT NULL);

	INSERT @ServicePackages ([InternalId], [Id], [Name], [Description], [PanelGroupId], [PackageLevel]) VALUES (1, 1, N'Premium', N'Premium', 0, 2)
	INSERT @ServicePackages ([InternalId], [Id], [Name], [Description], [PanelGroupId], [PackageLevel]) VALUES (2, 2, N'Basic', N'Basic', 0, 1)
	INSERT @ServicePackages ([InternalId], [Id], [Name], [Description], [PanelGroupId], [PackageLevel]) VALUES (3, 1, N'Premium', N'Premium', 1, 2)
	INSERT @ServicePackages ([InternalId], [Id], [Name], [Description], [PanelGroupId], [PackageLevel]) VALUES (4, 2, N'Basic', N'Basic', 1, 1)

	MERGE INTO dbo.ServicePackages AS TARGET  
	USING @ServicePackages AS SOURCE  
	ON TARGET.[InternalId] = SOURCE.[InternalId]
	WHEN MATCHED THEN    
	UPDATE SET 
		Id = SOURCE.Id,
		Name = SOURCE.Name,
		Description = SOURCE.Description,
		PanelGroupId = SOURCE.PanelGroupId,
		PackageLevel = SOURCE.PackageLevel
	WHEN NOT MATCHED BY TARGET THEN   
	INSERT ([InternalId], [Id], [Name], [Description], [PanelGroupId], [PackageLevel])  
	VALUES (SOURCE.InternalId, SOURCE.Id, SOURCE.Name, SOURCE.Description, SOURCE.PanelGroupId, SOURCE.PackageLevel)   
	OUTPUT $action, SOURCE.*;
	
	