USE [DataPro]
GO

/****** Object:  StoredProcedure [dbo].[sp_CompareGroupHardware]    Script Date: 30/01/2019 15:18:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sp_CompareGroupHardware]

	@StaticGroupId		INT,
	@EmbeddedGroupId	INT,
	@Result				BIT OUTPUT

AS
BEGIN
	BEGIN TRY
		DECLARE @tStaticHardware table (
				 DASId				INT
				,Processed			BIT)

		DECLARE @tEmbeddedHardware table (
				 DASId				INT)
	
		INSERT INTO @tStaticHardware
		SELECT 	DASId
			   ,0
		FROM [dbo].[GroupHardware] WHERE GroupId = @StaticGroupId ORDER BY DASId
	
		INSERT INTO @tEmbeddedHardware
		SELECT 	DASId
		FROM [dbo].[GroupHardware] WHERE GroupId = @EmbeddedGroupId ORDER BY DASId
	
		WHILE ((SELECT COUNT(*) From @tStaticHardware Where Processed = 0) > 0)
		BEGIN
			DECLARE @StaticDASId		INT
			DECLARE @EmbeddedDASId		INT

			SELECT TOP 1
						@StaticDASId	= DASId		
			FROM @tStaticHardware WHERE Processed = 0 

			IF ((SELECT COUNT(*) FROM @tEmbeddedHardware WHERE DASId = @StaticDASId) = 0)
			BEGIN
				SET @Result = 0
				RETURN;
			END

			UPDATE @tStaticHardware set [Processed] = 1 where DASId = @StaticDASId
		END
		SET @Result = 1
	END TRY
	BEGIN CATCH
		SET @Result = 0
	END CATCH
END


GO


