More SQL fixes