Use ATNDBTest
Go
------------------------------------------------------------------------------------------------------------
Update AccEntityKind Set 
	Code = Serial
Where 
	Code is Null or 
	Code = 0
Go
------------------------------------------------------------------------------------------------------------
Update AccEntityKind Set 
	OfficeDetailRef = 1
Where 
	OfficeDetailRef is Null or 
	OfficeDetailRef = 0
Go	
--------------------------------------------------------------------------------------------------------------
Declare @Recorder int
Declare @RecordDate varchar(10)
Set @Recorder = (Select RecorderRef From AccAdjutantL1CodeBook Where Serial = 1)
Set @RecordDate = (Select RecordDate From AccAdjutantL1CodeBook Where Serial = 1)

Update AccEntityKind Set
	RecorderRef = @Recorder,
	RecordDate = @RecordDate
--------------------------------------------------------------------------------------------------------------
---- اگه دفتر مالی دومی بود کد زیر اجرا گردد
---- دقت شود OfficeDetailRef به 
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (101, 1, N'اشخاص', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (102, 2, N'شرکتها', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (103, 3, N'مراکز هزينه', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (104, 4, N'پروژه ها', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (105, 5, N'قراردادهاي خريد خدمات', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (106, 6, N'قراردادهاي فروش خدمات', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (107, 7, N'اعتبارات اسنادي', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (108, 8, N'حسابهاي جاري', 2)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (109, 9, N'ساير', 2)

--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 101 Where AccEntityKindRef = 1  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 102 Where AccEntityKindRef = 2  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 103 Where AccEntityKindRef = 3  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 104 Where AccEntityKindRef = 4  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 105 Where AccEntityKindRef = 5  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 106 Where AccEntityKindRef = 6  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 107 Where AccEntityKindRef = 7  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 108 Where AccEntityKindRef = 8  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 109 Where AccEntityKindRef = 99 And OfficeDetailRef = 2

--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 101 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 1  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 102 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 2  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 103 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 3  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 104 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 4  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 105 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 5  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 106 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 6  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 107 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 7  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 108 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 8  And OfficeDetailRef = 2
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 109 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 99 And OfficeDetailRef = 2

--------------------------------------------------------------------------------------------------------------
---- اگه دفتر مالی سومی بود کد زیر اجرا گردد
---- دقت شود OfficeDetailRef به 
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (111, 1, N'اشخاص', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (112, 2, N'شرکتها', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (113, 3, N'مراکز هزينه', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (114, 4, N'پروژه ها', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (115, 5, N'قراردادهاي خريد خدمات', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (116, 6, N'قراردادهاي فروش خدمات', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (117, 7, N'اعتبارات اسنادي', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (118, 8, N'حسابهاي جاري', 3)
--INSERT [ATNDBTest].[dbo].[AccEntityKind] ([Serial], [Code], [Description], [OfficeDetailRef]) VALUES (119, 9, N'ساير', 3)

--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 101 Where AccEntityKindRef = 1  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 102 Where AccEntityKindRef = 2  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 103 Where AccEntityKindRef = 3  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 104 Where AccEntityKindRef = 4  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 105 Where AccEntityKindRef = 5  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 106 Where AccEntityKindRef = 6  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 107 Where AccEntityKindRef = 7  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 108 Where AccEntityKindRef = 8  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccEntityL1CodeBook] set AccEntityKindRef = 109 Where AccEntityKindRef = 99 And OfficeDetailRef = 3

--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 101 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 1  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 102 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 2  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 103 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 3  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 104 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 4  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 105 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 5  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 106 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 6  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 107 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 7  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 108 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 8  And OfficeDetailRef = 3
--update [ATNDBTest].[dbo].[AccAdjutantL1Bridge] set AccEntityKindRef = 109 From [ATNDBTest].[dbo].[AccAdjutantL1Bridge] As A inner Join [ATNDBTest].[dbo].[AccAdjutantL1CodeBook] As B on A.AccAdjutantL1CodeBookRef = B.serial  Where AccEntityKindRef = 99 And OfficeDetailRef = 3

--------------------------------------------------------------------------------------------------------------
