IF (NOT EXISTS (SELECT * FROM REF_CUSTOM_CONTROL WHERE [TableName] = N'DT_EMPLOYEE' AND [FieldName] = N'MANAGERREF'))
BEGIN
	INSERT INTO [dbo].[REF_CUSTOM_CONTROL] ([TableName], [FieldName], [CustomisationID], [Caption], [TabOrder], [ListOrder], [Enabled], [ControlTop], [ControlLeft], [ControlWidth], [ControlHeight], [Mask], [DefaultValue], [ControlType], [Compulsory], [LookupID], [ToolTipText], [ControlBackColor], [FontColor], [FontStyle], [FontBold], [FontItalic], [FontUnderline], [FontSize], [Visible], [MaximumLength], [CharacterTable], [Characters], [ShowCodesInCombo], [Justification], [MinValue], [MaxValue], [IntDigits], [DecDigits], [ValidateMask], [ShowZero], [fmtThousands], [EntityUnique], [TreatZeroAsBlank], [ObjectChooserTable], [OverideComboTextField], [MiscSelectorTable], [WidthOffset], [DisplayInHeader], [KeyField], [LabelAbove], [HideLabelOnFrame], [ImageMode], [ImageFilePattern], [ScaleImage], [MaintainAspect], [MiscSelectorSortFields], [MailMergeReport], [NULLDisplayField], [DisplayTab], [SecurityIdentifier], [ExcelTextOutput], [Hidden], [Locked], [DataProtectionTrigger])
	VALUES (N'DT_EMPLOYEE', N'MANAGERREF', 0, N'Manager Reference', 0, 0, 0, 0, 0, 4, 1, N'', NULL, 0, 0, NULL, N'Manager Reference', -2147483633, -2147483630, N'Calibri', 0, 0, 0, 9, 1, 0, N'', -1, 0, 0, NULL, NULL, NULL, NULL, 0, 1, N',', 0, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, N'', 0, 0, NULL, NULL, NULL, NULL, NULL, 0, 1, 0, 0)

	INSERT INTO [dbo].[REF_SQLFIELDS] ([Table_Name], [Field_Name], [CustomisationID], [Version], [Code], [OnByDefault], [InLine]) 
	VALUES (N'DT_EMPLOYEE', N'ManagerRef', 0, 1, N'SELECT TOP 1 CAST(DTE.EmployeeID as nvarchar(128))
	FROM DT_EMPLOYEE DTE
	INNER JOIN LNK_EMPLOYEE_CONTRACT LEC
	ON DTE.Entity_Id = LEC.Left_Join
	AND [CalcDate] BETWEEN LEC.Start_Date AND COALESCE(LEC.End_Date, [CalcDate])
	AND [CalcDate] BETWEEN DTE.Entity_Start_Date AND COALESCE(DTE.Entity_End_Date, [CalcDate])
	INNER JOIN LNK_JOB_CONTRACT_VIRTUAL LJC
	ON LJC.Right_Join = LEC.Right_Join
	AND [CalcDate] BETWEEN LJC.Start_Date AND COALESCE(LJC.End_Date, [CalcDate])
	AND LJC.Left_Join = Manager_Man.ManagingJob 
	ORDER BY LJC.Start_Date DESC{{J}}[#J#Manager]', 0, 1)
END