/****** Object:  StoredProcedure [dbo].[P_PDV_VD_CUPONS_VINCULAR_VENDA_EXECUTE_ALL]    Script Date: 11/07/2013 18:13:23 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_PDV_VD_CUPONS_VINCULAR_VENDA_EXECUTE_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_PDV_VD_CUPONS_VINCULAR_VENDA_EXECUTE_ALL]
GO
GO
GO
--exec P_PDV_VD_CUPONS_VINCULAR_VENDA_EXECUTE_ALL 1,'2018-01-01'
CREATE PROCEDURE [dbo].[P_PDV_VD_CUPONS_VINCULAR_VENDA_EXECUTE_ALL] @CD_EMP INT=1,@DT_PROC DATETIME=NULL AS
SET NOCOUNT ON
DECLARE @CD_FILIAL INT

IF @DT_PROC IS NULL
BEGIN
	SET @DT_PROC=CONVERT(CHAR(10),GETDATE(),112)
END

/*
	Tem por objetivo vincular o cupom fiscal com a pdv_vd_cupons
*/
DECLARE CCursorCuponsVincular CURSOR FAST_FORWARD FOR 
	SELECT 
		CD_FILIAL
	FROM
		PRC_FILIAL
	WHERE
		CD_EMP =@CD_EMP			
OPEN CCursorCuponsVincular
FETCH NEXT FROM CCursorCuponsVincular INTO @CD_FILIAL
	WHILE @@FETCH_STATUS=0
	  begin
		PRINT 'Vinculando filial.: ' + CONVERT(VARCHAR, @CD_FILIAL) + ' ' + CONVERT(VARCHAR, GETDATE (),108)

		EXEC P_PDV_VD_CUPONS_VINCULAR_VENDA @CD_EMP,@CD_FILIAL,@DT_PROC

		FETCH NEXT FROM CCursorCuponsVincular INTO @CD_FILIAL
	  end

PRINT 'Concluido.'
CLOSE CCursorCuponsVincular;
DEALLOCATE CCursorCuponsVincular;
