IF NOT OBJECT_ID('P_EST_NF_ENT_DANFE_PROVISORIO_LIBERACAO_ENTRADA_ITENS') IS NULL
        BEGIN
            DROP PROCEDURE P_EST_NF_ENT_DANFE_PROVISORIO_LIBERACAO_ENTRADA_ITENS 
        END
go
--EXEC P_EST_NF_ENT_DANFE_PROVISORIO_LIBERACAO_ENTRADA_ITENS 1,33260
CREATE PROCEDURE P_EST_NF_ENT_DANFE_PROVISORIO_LIBERACAO_ENTRADA_ITENS (@CD_PROD INT, 
																		@CD_USU INT)
AS 
DECLARE @CD_CTR INT
DECLARE @ERROS_VALIDACAO XML

DECLARE LiberarDanfeProblema CURSOR FAST_FORWARD FOR 
SELECT
	EST_NF_ENT_DANFE_PROVISORIO_ITENS.CD_CTR,
	EST_NF_ENT_DANFE_PROVISORIO_ERROS_VALIDACAO.ERROS_VALIDACAO
FROM
	EST_NF_ENT_DANFE_PROVISORIO_ITENS INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_ERROS_VALIDACAO ON
	EST_NF_ENT_DANFE_PROVISORIO_ITENS.CD_CTR = EST_NF_ENT_DANFE_PROVISORIO_ERROS_VALIDACAO.CD_CTR
WHERE
	EST_NF_ENT_DANFE_PROVISORIO_ITENS.CD_PROD = @CD_PROD


OPEN LiberarDanfeProblema
FETCH NEXT FROM LiberarDanfeProblema INTO 
				@CD_CTR,@ERROS_VALIDACAO

	WHILE @@FETCH_STATUS = 0 
		BEGIN	
			PRINT 'CONTROLE.: ' + CONVERT(VARCHAR(20),@CD_CTR) 		
			INSERT INTO EST_NF_ENT_DANFE_PROVISORIO_LIBERACAO_ENTRADA
			(CD_CTR, CD_USU, ERROS_XML)
			SELECT @CD_CTR, @CD_USU, @ERROS_VALIDACAO FROM EST_NF_ENT_DANFE_PROVISORIO_ERROS_VALIDACAO WHERE CD_CTR =@CD_CTR;

			DELETE FROM EST_NF_ENT_DANFE_PROVISORIO_ERROS_VALIDACAO WHERE CD_CTR = @CD_CTR;		

		FETCH NEXT FROM LiberarDanfeProblema INTO 
						@CD_CTR,@ERROS_VALIDACAO
		END
	    CLOSE LiberarDanfeProblema
	    DEALLOCATE LiberarDanfeProblema


