-- بر اساس یک اشتبــاه، داده های اولیه 4 جدول زیر با سریال کاربر دفاتر مالی نادرست از روی
-- جدول دفاتر مالی برداشته شده است. لذا ذیلا این مورد را اصلاح کرده و از روی جدول دوره مالی
--        کاربری که کوچکتری سریال را دارد برداشته و مجددا کاربر ثبت کننده را آپدیت میکنیم
-- 


Use ATNDBTest
Go

Update 
	PAY.Factor
Set
	RecorderRef = (Select min(RecorderRef) From OfficePeriod Where OfficePeriod.OfficeDetailRef = PAY.Factor.OfficeDetailRef)
From 
	PAY.Factor Inner Join
	(Select OfficeDetailRef, dbo.Concatenate(Serial) As Serial from dbo.Users Group by OfficeDetailRef) U On PAY.Factor.OfficeDetailRef = u.OfficeDetailRef
Where
	PAY.Factor.RecorderRef Not in (Select Items from dbo.SplitString(Ltrim(Rtrim(u.Serial)),','))





Update 
	dbo.FactorCategory
Set
	RecorderRef = (Select min(RecorderRef) From OfficePeriod Where OfficePeriod.OfficeDetailRef = dbo.FactorCategory.OfficeDetailRef)
From 
	dbo.FactorCategory Inner Join
	(Select OfficeDetailRef, dbo.Concatenate(Serial) As Serial from dbo.Users Group by OfficeDetailRef) U On dbo.FactorCategory.OfficeDetailRef = u.OfficeDetailRef
Where
	dbo.FactorCategory.RecorderRef Not in (Select Items from dbo.SplitString(Ltrim(Rtrim(u.Serial)),','))





Update 
	dbo.GetterOrPayerFactorCategory
Set
	RecorderRef = (Select min(RecorderRef) From OfficePeriod Where OfficePeriod.OfficeDetailRef = dbo.GetterOrPayerFactorCategory.OfficeDetailRef)
From 
	dbo.GetterOrPayerFactorCategory Inner Join
	(Select OfficeDetailRef, dbo.Concatenate(Serial) As Serial from dbo.Users Group by OfficeDetailRef) U On dbo.GetterOrPayerFactorCategory.OfficeDetailRef = u.OfficeDetailRef
Where
	dbo.GetterOrPayerFactorCategory.RecorderRef Not in (Select Items from dbo.SplitString(Ltrim(Rtrim(u.Serial)),','))





Update 
	dbo.Degree
Set
	RecorderRef = (Select min(RecorderRef) From OfficePeriod Where OfficePeriod.OfficeDetailRef = dbo.Degree.OfficeDetailRef)
From 
	dbo.Degree Inner Join
	(Select OfficeDetailRef, dbo.Concatenate(Serial) As Serial from dbo.Users Group by OfficeDetailRef) U On dbo.Degree.OfficeDetailRef = u.OfficeDetailRef
Where
	dbo.Degree.RecorderRef Not in (Select Items from dbo.SplitString(Ltrim(Rtrim(u.Serial)),','))
