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

	SELECT @CNT_DOCS = COUNT(*)
	FROM DOC D
	JOIN
	(
	   SELECT DOC_KEY
	   FROM DOC
	   WHERE PREV_REVISION = ''
	   GROUP BY DOC_KEY HAVING COUNT(*) > 1
	) T
	ON T.DOC_KEY = D.DOC_KEY
	WHERE D.PREV_REVISION = ''

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

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


	SELECT @STATUS, @MESSAGE
END

