Small sql bugfixes