/*
   چهارشنبه, 30 آبان 1403 - 14:20:22 ق.ظ
   User: 
   Server: TAHERIAN
   Database: ATNDB
   Application: 
*/

Use ATNDBTest
Go
/*=============================================================================================================*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT

BEGIN TRANSACTION
GO

ALTER TABLE dbo.FactorHeader ADD
	SumRebateInFactorDetail Bigint NULL
GO
ALTER TABLE dbo.FactorHeader SET (LOCK_ESCALATION = TABLE)
GO

COMMIT

Update 
	FactorHeader
Set 
	SumRebateInFactorDetail = IsNull(K.SumRebateInFactorDetail,0)
From 
	FactorHeader Inner Join 
	(Select 
		FactorHeaderRef As FactorHeaderSerial, 
		Sum(IsNull(RebatePrice,0)) As SumRebateInFactorDetail
	 From 
		FactorDetail  
	 Group By 
		FactorHeaderRef) K On K.FactorHeaderSerial = FactorHeader.Serial 