fixed bugs in the sql procedures