GO
IF EXISTS(SELECT * FROM SYS.OBJECTS
			WHERE  OBJECT_ID = OBJECT_ID(N'[DBO].[P_EST_PED_CP_ATUALIZA_STS_PED_EXEC_ALL]') AND TYPE IN(N'P', N'PC'))
DROP PROCEDURE [DBO].[P_EST_PED_CP_ATUALIZA_STS_PED_EXEC_ALL];
GO

GO

--P_EST_PED_CP_ATUALIZA_STS_PED_EXEC_ALL 1
CREATE PROCEDURE [dbo].[P_EST_PED_CP_ATUALIZA_STS_PED_EXEC_ALL](@CD_EMP           INT = 1
																, @DT_INI           DATETIME = NULL
																, @DT_FIM           DATETIME = NULL  )
AS
BEGIN
	SET NOCOUNT ON;
	SET ANSI_WARNINGS OFF;



	
	DECLARE @cd_chave int ;
							
	DECLARE c_Chave CURSOR FAST_FORWARD READ_ONLY FOR
	SELECT CD_FILIAL  
		FROM PRC_FILIAL 
		ORDER BY CD_FILIAL

	OPEN c_Chave
	FETCH NEXT FROM c_Chave INTO @cd_chave
	WHILE @@FETCH_STATUS = 0
		BEGIN
			PRINT 'Procesando lj ' + CONVERT(VARCHAR, @cd_chave)
				 
			EXEC P_EST_PED_CP_ATUALIZA_STS_PED @CD_EMP,  @CD_CHAVE, @DT_INI,@DT_FIM,0;

			FETCH NEXT FROM c_Chave INTO @cd_chave
		END
	CLOSE c_Chave;
	DEALLOCATE c_Chave; 
	

END