SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create TRIGGER [dbo].[AccFactorDetail_CheckDelete]
ON [dbo].[AccFactorDetail] 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.AccFactorDetail where id=@ID
		END
		else
		    RAISERROR('امکان حذف فاکتور ارسال شده به سامانه نیست.',16,20) WITH LOG, SETERROR
	return