/****** Object:  StoredProcedure [dbo].[P_EST_NF_ENT_EST_NF_SAI_TRANSF_VINCULA_XML_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_EST_NF_SAI_TRANSF_VINCULA_XML_EXECUTE_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_EST_NF_ENT_EST_NF_SAI_TRANSF_VINCULA_XML_EXECUTE_ALL]
GO
GO
GO
--exec P_EST_NF_ENT_EST_NF_SAI_TRANSF_VINCULA_XML_EXECUTE_ALL 1, 9,2016
CREATE PROCEDURE [dbo].[P_EST_NF_ENT_EST_NF_SAI_TRANSF_VINCULA_XML_EXECUTE_ALL] @CD_EMP INT=1,@MES INT=0,@ANO INT=0 AS
SET NOCOUNT ON
DECLARE @CDEMP INT
DECLARE @CDFILIAL INT
DECLARE @ANOPROC INT
DECLARE @DTINI DATETIME
SET @ANOPROC=@ANO * 100 + @MES
SET @DTINI= CONVERT(VARCHAR, @ANOPROC) + '01'

DECLARE FilialVinculaXML CURSOR FAST_FORWARD FOR 
	SELECT 
		CD_EMP,
		CD_FILIAL
	FROM
		PRC_FILIAL
	ORDER BY
		CD_FILIAL
						
OPEN FilialVinculaXML
FETCH NEXT FROM FilialVinculaXML INTO @CD_EMP,@CDFILIAL
	WHILE @@FETCH_STATUS=0
	  begin
		PRINT 'Vinculando xml nas notas fiscais de transferencia filial.: ' + CONVERT(VARCHAR, @CDFILIAL) + ' ' + CONVERT(VARCHAR, GETDATE (),108)

		EXEC P_EST_NF_ENT_EST_NF_SAI_TRANSF_VINCULA_XML @CD_EMP,@CDFILIAL,@DTINI

		FETCH NEXT FROM FilialVinculaXML INTO @CD_EMP,@CDFILIAL
	  end

PRINT 'Concluido.'
CLOSE FilialVinculaXML;
DEALLOCATE FilialVinculaXML;
