IF OBJECT_ID('SP_CUPOMFISCAL_CONSULTAR') IS NOT NULL 
	DROP PROCEDURE SP_CUPOMFISCAL_CONSULTAR 
GO 

CREATE PROCEDURE SP_CUPOMFISCAL_CONSULTAR ( @CD_EMP INT, @CD_FILIAL INT, @DT_VD DATETIME, @CD_CX INT, @NR_ECF INT ) 
AS 
BEGIN 

DECLARE @CD_VD INT

SET @CD_VD = (SELECT TOP 1 CD_VD FROM PDV_VD VD WHERE VD.DT_VD = @DT_VD AND VD.CD_FILIAL = @CD_FILIAL AND VD.CD_EMP = @CD_EMP AND VD.CD_CX = @CD_CX AND VD.NR_ECF = @NR_ECF)

SELECT *
FROM 	V_CUPOMFISCAL VD
WHERE 	VD.DT_VD = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD= @CD_VD

SELECT *
FROM 	V_CUPOMFISCAL_ITEM VD 
WHERE 	VD.DT_VD = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD= @CD_VD

SELECT *
FROM 	V_CUPOMFISCAL_CARTAO VD 
WHERE 	VD.DT_VD = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD= @CD_VD

SELECT *
FROM 	V_CUPOMFISCAL_CREDIARIO VD 
WHERE 	VD.DT_VD = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD= @CD_VD

SELECT *
FROM 	V_CUPOMFISCAL_CHEQUE VD 
WHERE 	VD.DT_VD = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD= @CD_VD

SELECT *
FROM 	V_CUPOMFISCAL_PBM VD
WHERE 	VD.DT_VD = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD= @CD_VD

SELECT *
FROM 	V_CUPOMFISCAL_VALE_COMPRA VD 
WHERE 	VD.DT_VD = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD= @CD_VD 
SELECT *
FROM 	V_CUPOMNAOFISCAL VD 
WHERE 	VD.DT_CUPOM = @DT_VD
AND 	VD.CD_FILIAL = @CD_FILIAL
AND 	VD.CD_EMP = @CD_EMP
AND 	VD.CD_VD_DOACAO = @CD_VD 
END