
DECLARE @PanelInformationPanelTypeVersionRanges TABLE (
	[PanelDeviceType] [nvarchar](50) NULL,
	MinimumVersion int not NULL,
	MaximumVersion int NULL,
	[guid] [uniqueidentifier] NOT NULL
)

insert into @PanelInformationPanelTypeVersionRanges values ('C8000',1000000,null,'7BF40428-29B3-4DAC-A002-982316F41528');

MERGE INTO [dbo].PanelInformationPanelTypeVersionRanges AS TARGET  
USING @PanelInformationPanelTypeVersionRanges AS SOURCE  
ON TARGET.[guid] = SOURCE.[guid]
WHEN NOT MATCHED BY TARGET THEN
INSERT ([PanelDeviceType],MinimumVersion ,MaximumVersion ,[guid])
VALUES (SOURCE.[PanelDeviceType],SOURCE.MinimumVersion ,SOURCE.MaximumVersion ,SOURCE.[guid])
WHEN MATCHED THEN    
UPDATE SET	TARGET.[PanelDeviceType] = source.[PanelDeviceType],
			TARGET.MinimumVersion  = source.MinimumVersion ,
			TARGET.MaximumVersion  = source.MaximumVersion ,
			TARGET.[guid] 			 = source.[guid] 

OUTPUT $action, SOURCE.*;



DECLARE @PanelInformationPanelVersions TABLE (
	ValidationSchemaId [int] NOT NULL,
	PanelVersionRangeId [int] NOT NULL
	)

insert into @PanelInformationPanelVersions (ValidationSchemaId ,PanelVersionRangeId )
select 1,r.id
from PanelInformationPanelTypeVersionRanges r
where r.[guid] in ('7BF40428-29B3-4DAC-A002-982316F41528')

MERGE INTO [dbo].PanelInformationPanelVersions AS TARGET  
USING @PanelInformationPanelVersions AS SOURCE  
ON TARGET.ValidationSchemaId = SOURCE.ValidationSchemaId and target.PanelVersionRangeId =source.PanelVersionRangeId 
WHEN NOT MATCHED BY TARGET THEN
INSERT (ValidationSchemaId,PanelVersionRangeId)
VALUES (SOURCE.ValidationSchemaId, SOURCE.PanelVersionRangeId)
WHEN MATCHED THEN    
UPDATE SET 
target.ValidationSchemaId = SOURCE.ValidationSchemaId, 
target.PanelVersionRangeId = SOURCE.PanelVersionRangeId


OUTPUT $action, SOURCE.*;
