Bugfixes in sql