
DECLARE @PanelTypeVersionRanges TABLE (
	[PanelDeviceType] [nvarchar](50) NULL,
	[MinimalVersion] [nvarchar](50) NULL,
	[MaximalVersion] [nvarchar](50) NULL,
	[guid] [uniqueidentifier] NOT NULL
)

insert into @PanelTypeVersionRanges values ('C8000','1.0.0',null,'23106F30-2500-49E9-A738-7E05C54C765E');

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

OUTPUT $action, SOURCE.*;


DECLARE @PanelCommandTypePanelTypes TABLE (
	[PanelCommandTypeId] [int] NOT NULL,
	[PanelTypeVersionRangeId] [int] NOT NULL
	)

insert into @PanelCommandTypePanelTypes ([PanelCommandTypeId] ,[PanelTypeVersionRangeId] )
select c.id,r.id
from PanelCommandTypes c, PanelTypeVersionRanges r
where c.[guid] in ('F333E1F6-F9E6-4568-A1D3-E622446B357A','1EAD1AE7-6506-4B5D-9DD8-431D240C9133','64E7F69C-3F76-41F6-92A2-66694FD396F3', '7c80a196-87ae-41dd-865a-b90f66ffa93c','F333E1F6-F9E6-4568-A1D3-E622446B3333') 
  and r.[guid] in ('23106F30-2500-49E9-A738-7E05C54C765E')

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


OUTPUT $action, SOURCE.*;
