Loop didn't work in SQL