/****** Object:  StoredProcedure [dbo].[P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE_EXECUTE_ALL]    Script Date: 11/07/2013 18:13:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE_EXECUTE_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE_EXECUTE_ALL]
GO
GO
GO
--exec P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE_EXECUTE_ALL 1
CREATE PROCEDURE [dbo].[P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE_EXECUTE_ALL] @CD_EMP INT=1, 
																											@DT_INI AS DATETIME=NULL, 
																											@DT_FIM DATETIME=NULL AS
SET NOCOUNT ON;
IF @DT_INI IS NULL
BEGIN
	SET @DT_FIM=CONVERT(CHAR(10),GETDATE(),112)
	SET @DT_INI=@DT_FIM-20
END

DECLARE @CD_FILIAL INT
DECLARE FilialPendenciaConfirmacao CURSOR FAST_FORWARD FOR 
	SELECT 
		CD_FILIAL
	FROM
		PRC_FILIAL
	ORDER BY
		CD_FILIAL
						
OPEN FilialPendenciaConfirmacao
FETCH NEXT FROM FilialPendenciaConfirmacao INTO @CD_FILIAL
	WHILE @@FETCH_STATUS=0
	  begin
		PRINT 'Analisando filial.: ' + CONVERT(VARCHAR, @CD_FILIAL) + ' ' + CONVERT(VARCHAR, GETDATE (),108)

		EXEC P_EST_NF_ENT_DANFE_PROVISORIO_EST_NF_ENT_CONFIRMACAO_OPERACAO_PENDENTE @CD_EMP,@CD_FILIAL,@DT_INI,@DT_FIM

		FETCH NEXT FROM FilialPendenciaConfirmacao INTO @CD_FILIAL
	  end

PRINT 'Concluido.'
CLOSE FilialPendenciaConfirmacao;
DEALLOCATE FilialPendenciaConfirmacao;
