
DECLARE @ServicePackageHardwares TABLE 
( [DeviceTypeId] int not null, [ControlPanelServicePackageId] int not null, [ServicePackageId] int not null)

--pers for cp (deviceType=1)
insert into @ServicePackageHardwares ([DeviceTypeId],[ControlPanelServicePackageId],[ServicePackageId])
select 4,h.ControlPanelServicePackageId,h.ServicePackageId
from ServicePackageHardwares h
	join ServicePackages s on s.id=h.ServicePackageId
where s.CodeName in ('Pro','Family','Pers-e')
	and DeviceTypeId=2
group by h.ControlPanelServicePackageId,h.ServicePackageId

MERGE INTO [dbo].ServicePackageHardwares AS TARGET 
USING @ServicePackageHardwares AS SOURCE 
ON TARGET.[ServicePackageId] = SOURCE.[ServicePackageId] and target.[ControlPanelServicePackageId] = source.[ControlPanelServicePackageId] and target.[DeviceTypeId]=source.[DeviceTypeId] 
WHEN NOT MATCHED BY TARGET THEN
INSERT ([ServicePackageId], [ControlPanelServicePackageId],[DeviceTypeId])
VALUES (SOURCE.[ServicePackageId], SOURCE.[ControlPanelServicePackageId],source.[DeviceTypeId])

OUTPUT $action, SOURCE.*;