
IF NOT OBJECT_ID('P_ATUALIZA_DATA_CHEQUE_A_VISTA_NAO_FISCAL') IS NULL
	BEGIN 
		DROP PROCEDURE [dbo].[P_ATUALIZA_DATA_CHEQUE_A_VISTA_NAO_FISCAL]
	END
GO

CREATE PROCEDURE [dbo].[P_ATUALIZA_DATA_CHEQUE_A_VISTA_NAO_FISCAL]
AS
DECLARE @CD_FILIAL INT
DECLARE @CD_VD INT
DECLARE @DATA DATETIME

DECLARE DB_CURSOR CURSOR FOR  
SELECT
    CH.CD_FILIAL,CH.CD_CTR,C.DT_CUPOM

FROM PDV_CUPOM_NAO_FISCAL_CHQS CH INNER JOIN PDV_CUPOM_NAO_FISCAL C ON
    CH.CD_EMP=C.CD_EMP AND
    CH.CD_FILIAL=C.CD_FILIAL AND
    CH.CD_CTR=C.CD_CTR
WHERE CH.DT_EMI <> C.DT_CUPOM AND C.DT_CUPOM > '20171218'


OPEN DB_CURSOR   
FETCH NEXT FROM DB_CURSOR INTO  @CD_FILIAL,@CD_VD,@DATA

WHILE @@FETCH_STATUS = 0   
BEGIN   
    UPDATE PDV_CUPOM_NAO_FISCAL_CHQS SET DT_EMI=@DATA,DT_VENCTO=@DATA 
    WHERE CD_EMP=1 AND CD_FILIAL =@CD_FILIAL AND CD_CTR =@CD_VD
	         
    FETCH NEXT FROM DB_CURSOR INTO  @CD_FILIAL,@CD_VD,@DATA

END   

CLOSE DB_CURSOR   
DEALLOCATE DB_CURSOR;


GO


