How To Avoid While Loop In Sql Server