IF EXISTS( SELECT * FROM SYS.OBJECTS 
			WHERE NAME ='TRG_EST_PED_CP_CANCELA_PEDIDO'AND TYPE = 'TR')
BEGIN
	DROP TRIGGER DBO.TRG_EST_PED_CP_CANCELA_PEDIDO
END
GO
--------------------------------------CANCELAR PEDIDO DE COMPRAS EM EST_PED_CP--------------------------------------
CREATE TRIGGER [dbo].[TRG_EST_PED_CP_CANCELA_PEDIDO] ON [dbo].[EST_PED_CP] FOR UPDATE AS
IF UPDATE(STS_PED)
begin
IF EXISTS (
SELECT
	v.CD_CTR
FROM
	EST_NF_ENT_DANFE_PROVISORIO_EST_PED_CP v INNER JOIN EST_NF_ENT_DANFE_PROVISORIO_PENDENTE pend ON
	v.CD_CTR = pend.CD_CTR
	INNER JOIN INSERTED i ON
	v.CD_EMP = i.CD_EMP AND
	v.CD_FILIAL = i.CD_FILIAL AND
	v.CD_PED = i.CD_PED
WHERE
	i.STS_PED=2)
begin
	RAISERROR ('Pedido de compras possui vinculo com danfe pendente de entrada.',16, 1);
	ROLLBACK TRANSACTION;
	RETURN;
end
end