Updated to the new version of sql apply