/****** OBJECT:  STOREDPROCEDURE [DBO].[P_EST_PROD_EXTRATO_CALC_EXECUTE_ALL]    SCRIPT DATE: 06/16/2012 15:18:49 ******/
IF  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[P_EST_PROD_EXTRATO_CALC_EXECUTE_ALL]') AND TYPE IN (N'P', N'PC'))
DROP PROCEDURE [DBO].[P_EST_PROD_EXTRATO_CALC_EXECUTE_ALL]
GO
--EXEC P_EST_PROD_EXTRATO_CALC_EXECUTE_ALL 0, 1, 500
--VERSAO 9999
CREATE PROCEDURE [DBO].[P_EST_PROD_EXTRATO_CALC_EXECUTE_ALL] (@TP_EST INT = 0, @CD_EMP INT=1, @CdFilialPar INT=0) AS

PRINT 'AJUSTE DE ESTOQUE PELA MOVIMENTAÇÃO PARA TODAS AS FILIAIS '  
SET NOCOUNT ON
DECLARE @CD_FILIAL INT

DECLARE FILIAIS CURSOR FAST_FORWARD FOR 
	SELECT 
		CD_FILIAL 
	FROM 
		PRC_FILIAL
	WHERE
		CD_FILIAL = CASE WHEN @cdFilialPar=0 THEN CD_FILIAL ELSE @cdFilialPar END 
		AND STS_FILIAL = 0
	ORDER BY 
		CD_FILIAL

OPEN FILIAIS
FETCH NEXT FROM FILIAIS INTO @CD_FILIAL
	WHILE @@FETCH_STATUS=0
	  BEGIN
		
		PRINT 'FILIAL ' + CONVERT(VARCHAR, @CD_FILIAL) + ' ' + CONVERT(VARCHAR, GETDATE(),108) 

		EXEC P_EST_PROD_EXTRATO_CALC @CD_EMP,@CD_FILIAL, @TP_EST
		FETCH NEXT FROM FILIAIS INTO @CD_FILIAL
	  END

PRINT 'CONCLUÍDO.'
CLOSE FILIAIS
DEALLOCATE FILIAIS
