Refactor server sql