Again some sql fixes

I hope it works now