More SQL fixes.

These may have side effects, need to test.