-- select dbo.fGetMablaghFactor(7493,28) alter function fGetMablaghFactor(@Bar_ID int , @Dom_ID_Noe smallint) returns decimal(19,4) as begin declare @t decimal(19,4); set @t= isnull(( select sum((d.mablagh*d.meghdar1*d.meghdar2*d.tedad) + (case when d.Taf_ID_ArzeshAfzoodeh is not null then isnull(d.MablaghArzeshAfzoodeh,0) else 0 end) - (case when d.Taf_ID_Takhfif is not null then isnull(d.MablagheTakhfif,0) else 0 end)) from bargeh m inner join bargehDetail d on m.ID = d.Bar_ID and m.ID = @Bar_ID ),0); select @t = @t + isnull(MablaghEzafeh,0) from Bargeh where Taf_ID_Ezafeh is not null and ID = @Bar_ID select @t = @t - isnull(MablaghTakhfif,0) from Bargeh where Taf_ID_Takhfif is not null and ID = @Bar_ID return @t end