Moved hardcoded SQL to stored procedure.