I think we all agree that testing stored procedures and functions on a database tier can be time consuming, they are hard to debug and sometimes just difficult to get clarity on what is “happening”. To help you understand what’s going on, you can use the built-in debugging features of Microsoft SQL Management Studio. These allow you to see exactly what is going on, and step through your logic in a similar fashion as in Visual Studio.
To get you going check this post(Debugging SQL Queries, Functions, & Stored Procedures with SQL Management Studio’s Integrated Debugger) by Doug Rathbone.