SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create TRIGGER [dbo].[AccFactorPay_CheckDelete]
ON [dbo].[AccFactorPay] INSTEAD OF DELETE 
	AS
		DECLARE @TaxUID as UNIQUEIDENTIFIER
		DECLARE @FactorID as bigint
		DECLARE @ID as bigint
		select @ID=ID, @FactorID=FactorID from Deleted
		select @TaxUID=TaxUID from AccFactor with(nolock) where ID=@FactorID 
		if @TaxUID = CAST(0x0 AS UNIQUEIDENTIFIER) 
		BEGIN
			delete From dbo.AccFactorPay where id=@ID
		END
		else
		    RAISERROR('امکان حذف فاکتور ارسال شده به سامانه نیست.',16,20) WITH LOG, SETERROR
	return