
-- Convert single Filter from Subscribers table to filter list in SubscriberFilters table

DECLARE @SubscriberFiltersTemp TABLE (SubscriberId int NOT NULL,FilterId int not null,[order] int NOT NULL);

	--every subscriber has only one filter thus order=1
	INSERT into @SubscriberFiltersTemp (SubscriberId,FilterId , [Order]) select Id, FilterId, 1 from Subscribers


	MERGE INTO dbo.SubscriberFilters AS TARGET  
		USING @SubscriberFiltersTemp AS SOURCE  
		ON TARGET.SubscriberId = SOURCE.SubscriberId and TARGET.FilterId = SOURCE.FilterId 
		--WHEN MATCHED THEN    
			
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (SubscriberId,FilterId , [Order]) VALUES (SOURCE.SubscriberId, SOURCE.FilterId, SOURCE.[Order])    
	OUTPUT $action, SOURCE.*;




