--EXEC MIG_MULTIPLE_REVISIONS_WITH_PREV_REVISION_EMPTY_ITEM_Sp '', ''
CREATE OR ALTER PROCEDURE MIG_MULTIPLE_REVISIONS_WITH_PREV_REVISION_EMPTY_ITEM_Sp
(
	@STATUS INT = 0 OUT,
	@MESSAGE NVARCHAR(999) = NULL OUT
)
AS 
BEGIN
	DECLARE @CNT_ITEMS INT
	
	SET @STATUS = 0
	SET @MESSAGE = NULL

	SELECT @CNT_ITEMS = COUNT(*)
	FROM ITEM D
	JOIN
	(
	   SELECT ITEM_KEY
	   FROM ITEM
	   WHERE PREV_REVISION = ''
	   GROUP BY ITEM_KEY HAVING COUNT(*) > 1
	) T
	ON T.ITEM_KEY = D.ITEM_KEY
	WHERE D.PREV_REVISION = ''

	IF @CNT_ITEMS > 0
		BEGIN
			SET @STATUS = 1
			IF @MESSAGE IS NULL
				SET @MESSAGE = ''

			SET @MESSAGE = @MESSAGE + 'ITEMS with more than one revision having PREV_REVISION field value as empty. '
		END


	SELECT @STATUS, @MESSAGE
END

