IF EXISTS(SELECT * FROM sys.objects
			WHERE object_id = OBJECT_ID(N'[dbo].[P_TLV_PED_VD_ATU_STS_PED]')
		  AND type IN(N'P', N'PC'))
	DROP PROCEDURE [dbo].[P_TLV_PED_VD_ATU_STS_PED];
GO

CREATE PROCEDURE [dbo].[P_TLV_PED_VD_ATU_STS_PED]
as

UPDATE TLV_PED_VD
SET TLV_PED_VD.STS_PED ='D' 
FROM         
	TLV_PED_VD 
	INNER JOIN TLV_PED_VD_PDV_VD ON 
	TLV_PED_VD.CD_EMP = TLV_PED_VD_PDV_VD.CD_EMP AND 
	TLV_PED_VD.CD_FILIAL = TLV_PED_VD_PDV_VD.CD_FILIAL AND 
	TLV_PED_VD.CD_PED_VD = TLV_PED_VD_PDV_VD.CD_PED_VD 
	INNER JOIN PDV_VD_EST_NF_ENT ON 
	TLV_PED_VD_PDV_VD.CD_EMP = PDV_VD_EST_NF_ENT.CD_EMP AND 
	TLV_PED_VD_PDV_VD.CD_VD = PDV_VD_EST_NF_ENT.CD_VD AND 
	TLV_PED_VD_PDV_VD.CD_FILIAL_PDV_VD = PDV_VD_EST_NF_ENT.CD_FILIAL
WHERE     
	TLV_PED_VD.STS_PED <> 'D'
	AND TLV_PED_VD.CD_EMP=1
	AND TLV_PED_VD.DT_PED_VD> GETDATE()-60
GO
