Fixed sql errors from master

Repaired the sql functions from the master branch