Replaced stored proc with plain tsql