Fix exception on sql building