

BEGIN TRY
	BEGIN
		DECLARE	@CD_FILIAL_RS	INT				
					
		--Criando cursor
		DECLARE FILIAIS CURSOR FAST_FORWARD FOR 
		Select  
			DISTINCT		
				CD_FILIAL
			From    
				EST_PROD_TRANSF_LACRE

		
		OPEN FILIAIS;
		FETCH NEXT FROM FILIAIS INTO @CD_FILIAL_RS;
		PRINT 'PROCESSANDO FILIAIS: ';
		
		WHILE @@FETCH_STATUS = 0
		  begin
			PRINT CONVERT(VARCHAR(3), @CD_FILIAL_RS) + ', ';
			
			UPDATE L  SET
				CD_LACRE = T.CD_LACRE 
			FROM EST_PROD_TRANSF_LACRE L
				INNER JOIN (SELECT T.CD_EMP, T.CD_FILIAL, T.CD_CTR, T.NR_LACRE, 
								  (ROW_NUMBER() OVER (PARTITION BY T.CD_EMP, T.CD_FILIAL, T.CD_CTR ORDER BY T.CD_CTR)) AS CD_LACRE
							FROM EST_PROD_TRANSF_LACRE T
							WHERE T.CD_LACRE IS NULL) T ON
						L.CD_EMP			= T.CD_EMP 
						AND L.CD_FILIAL		= T.CD_FILIAL  
						AND L.CD_CTR		= T.CD_CTR  
						AND L.NR_LACRE		= T.NR_LACRE 
				
			WHERE L.CD_FILIAL = @CD_FILIAL_RS;								
			------------------------------------------------------------------------------------------------
			
			UPDATE LI  SET
				CD_LACRE = L.CD_LACRE				
			FROM EST_PROD_TRANSF_LACRE L
				INNER JOIN EST_PROD_TRANSF_LACRE_IT LI ON 
					L.CD_EMP			= LI.CD_EMP
					AND L.CD_FILIAL		= LI.CD_FILIAL 
					AND L.CD_CTR		= LI.CD_CTR 
					AND L.NR_LACRE		= LI.NR_LACRE 
			WHERE COALESCE(LI.CD_LACRE,0) = 0
				AND L.CD_FILIAL = @CD_FILIAL_RS;								
			------------------------------------------------------------------------------------------------
			
			UPDATE LI  SET
				CD_LACRE = L.CD_LACRE 
			FROM EST_PROD_TRANSF_LACRE L
				INNER JOIN EST_PROD_TRANSF_LACRE_CHECKIN_ENTRADA LI ON
					L.CD_EMP			= LI.CD_EMP
					AND L.CD_FILIAL		= LI.CD_FILIAL 
					AND L.CD_CTR		= LI.CD_CTR 
					AND L.NR_LACRE		= LI.NR_LACRE 
			WHERE COALESCE(LI.CD_LACRE,0) = 0
				AND L.CD_FILIAL = @CD_FILIAL_RS;								
			------------------------------------------------------------------------------------------------
			
			UPDATE LI  SET
				CD_LACRE = L.CD_LACRE 
			FROM EST_PROD_TRANSF_LACRE L
				INNER JOIN EST_PROD_TRANSF_LACRE_IT_CHECKIN_ENTRADA LI ON
					L.CD_EMP			= LI.CD_EMP
					AND L.CD_FILIAL		= LI.CD_FILIAL 
					AND L.CD_CTR		= LI.CD_CTR 
					AND L.NR_LACRE		= LI.NR_LACRE 
			WHERE COALESCE(LI.CD_LACRE,0) = 0
				AND L.CD_FILIAL = @CD_FILIAL_RS;											
			------------------------------------------------------------------------------------------------
			
			UPDATE LI  SET
				CD_LACRE = L.CD_LACRE 
			FROM EST_PROD_TRANSF_LACRE L
				INNER JOIN EST_PROD_TRANSF_LACRE_CHECKIN_ENTRADA_TMP LI ON
					L.CD_EMP			= LI.CD_EMP
					AND L.CD_FILIAL		= LI.CD_FILIAL 
					AND L.CD_CTR		= LI.CD_CTR 
					AND L.NR_LACRE		= LI.NR_LACRE 
			WHERE COALESCE(LI.CD_LACRE,0) = 0
				AND L.CD_FILIAL = @CD_FILIAL_RS;											
			------------------------------------------------------------------------------------------------
			
			UPDATE LI  SET
				CD_LACRE = L.CD_LACRE 
			FROM EST_PROD_TRANSF_LACRE L
				INNER JOIN EST_PROD_TRANSF_LACRE_IT_CHECKIN_ENTRADA_TMP LI ON
					L.CD_EMP			= LI.CD_EMP
					AND L.CD_FILIAL		= LI.CD_FILIAL 
					AND L.CD_CTR		= LI.CD_CTR 
					AND L.NR_LACRE		= LI.NR_LACRE 
			WHERE COALESCE(LI.CD_LACRE,0) = 0
				AND L.CD_FILIAL = @CD_FILIAL_RS;										
			------------------------------------------------------------------------------------------------			
			
			FETCH NEXT FROM FILIAIS INTO @CD_FILIAL_RS;
		  end
		  
		CLOSE FILIAIS;
		DEALLOCATE FILIAIS;	
	END
END TRY

BEGIN CATCH
	CLOSE FILIAIS
	DEALLOCATE FILIAIS	
END CATCH
