Fixed a bug in sql code