Error in SQL statement expansion? And how to correct it?