/****** Object:  StoredProcedure [dbo].[P_PDV_VD_CREDIARIO_CANCELADO_ALL]    Script Date: 11/28/2013 09:36:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[P_PDV_VD_CREDIARIO_CANCELADO_ALL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[P_PDV_VD_CREDIARIO_CANCELADO_ALL]
GO
GO
--EXEC P_PDV_VD_CREDIARIO_CANCELADO_ALL 1
CREATE PROCEDURE P_PDV_VD_CREDIARIO_CANCELADO_ALL @CD_EMP INT, @DT_INI DATETIME=null, @DT_FIM DATETIME=null
AS
SET NOCOUNT ON
DECLARE @CD_FILIAL INT

if @DT_INI is null
begin
	set @DT_INI=convert(char(10),getdate()-10,112)
end

if @DT_FIM is null
begin
	set @DT_FIM=convert(char(10),getdate(),112)
end


DECLARE ProcFilialVendasCanceladas CURSOR FAST_FORWARD FOR 
SELECT
	CD_FILIAL
FROM
	PRC_FILIAL
WHERE
	CD_EMP=@CD_EMP

OPEN ProcFilialVendasCanceladas;
FETCH NEXT FROM ProcFilialVendasCanceladas INTO @CD_FILIAL;
WHILE @@FETCH_STATUS = 0
    BEGIN
		PRINT 'Filial.: ' + CONVERT(VARCHAR(10),@CD_FILIAL)
		
		EXEC P_PDV_VD_CREDIARIO_CANCELADO  @CD_EMP,@CD_FILIAL,@DT_INI,@DT_FIM

		FETCH NEXT FROM ProcFilialVendasCanceladas INTO @CD_FILIAL;
    END;
PRINT 'CONCLUIDO.';
CLOSE ProcFilialVendasCanceladas;
DEALLOCATE ProcFilialVendasCanceladas;