Merged SQL-fixes from Tollef.