• Forums are designed for short, quick, targeted advice since they are 'staffed' by volunteers. This isn't any of those. 🙂 It could take many hours of time to help you refactor this sproc, and I advise you to look at getting some professional help to do it.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service