DECLARE @ParentSurfaceItemId int, @itemID int, @currentSchoolName nvarchar(255);
SET rowcount 0
select itemID, ParentSurfaceItemId, SchoolContacts_SchoolContacts_SchoolName into #tempTable from [publ_SchoolContacts] ORDER BY recId;

SET rowcount 1
select @itemID = itemID from #tempTable;
select @ParentSurfaceItemId = ParentSurfaceItemId from #tempTable;
select @currentSchoolName = SchoolContacts_SchoolContacts_SchoolName from #tempTable;

WHILE @@rowcount <> 0
BEGIN
    SET rowcount 0
	delete #tempTable where itemID = @itemID
	DECLARE @schoolName nvarchar(255), @schoolTypeId int, @schoolType nvarchar(255);

	IF LEN(@currentSchoolName) = 0
	BEGIN
		SELECT @schoolName = Schools_SchoolDetails_SchoolName, @schoolTypeId = Schools_SchoolDetails_SchoolType FROM [publ_Schools] WHERE itemID = @ParentSurfaceItemId;
		IF LEN(@schoolName) > 0
		BEGIN
			SELECT @schoolType = display FROM [pal_SurfaceLookup] WHERE recId = @schoolTypeId;
			IF LEN(@schoolType) > 0
			BEGIN
				UPDATE [publ_SchoolContacts] SET SchoolContacts_SchoolContacts_SchoolName = @schoolName, SchoolContacts_SchoolContacts_SchoolType = @schoolType, SchoolContacts_SchoolContacts_ActiveYear = '2019-01-01 00:00:00.000' WHERE itemID = @itemID;
			END;

		END;
	END;
	SET rowcount 1
	select @itemID = itemID from #tempTable;
	select @ParentSurfaceItemId = ParentSurfaceItemId from #tempTable;
	select @currentSchoolName = SchoolContacts_SchoolContacts_SchoolName from #tempTable;

END
set rowcount 0;
DROP TABLE #tempTable;