Debugging Stored Procedures
Business logic in a software application inevitably is spread out over servers, web servers, web services, assemblies, etc. Some logic may be in a C# assembly while other portions of the logic lie in SQL Server. In Visual Studio, debugging the C# code is a piece of cake (I didn't say fun J, just simple to step through code).
Transact SQL has always been another animal. "If I had a nickel for every print statement…" It generally involves opening SQL Management Studio, testing smaller statements, creating a larger stored procedure and executing the stored procedure on its own.
Visual Studio has a couple tools to make the entire SQL debug thing about as easy as C# code. It's true, no smiley face! We'll get into two main topics. The first is debugging a stored procedure on its own. Second (ready for this?), actually stepping through C# code and directly into the stored procedure when called by the C# code. Let's go!
The Stored Procedure
- Start the SQL Management Studio
- Must log with Windows Authentication with sysadmin privileges
- Create a database called Testing
In the Testing database, create a stored procedure to simply return the current date
CREATE PROCEDURE [dbo].[GET_CURRENT_DATE] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON -- Insert statements for procedure here SELECT GETDATE() END GO
Debugging the Stored Procedure
- Start Visual Studio
- Connect to the Testing Database
Open the Server Explorer (Ctrl + Alt + S)
- Right click Data Connections
- Click Add Connection
- In the Add Connection Dialog, connect to the Testing database
- Use Windows Authentication
- Again, need the sysadmin privileges
- Click OK
Back in the Server Explorer
- Expand the server
- Right click the server and ensure Application Debugging is checked
- Expand Stored Procedures
- Double click the GetTheCurrentDate procedure
- This opens the stored procedure in the editor window
- Right click some white space and click Step Into Stored Procedure
- F11, Local Variables, Watch, Breakpoints, all of it works the same as C#
- How cool is that!
Stepping from C# into the Stored Procedure
- Create a console application
- In the Solution Explorer, double click the Properties folder
- Click the Debug tab
- Ensure Enable SQL Server Debugging is checked
In the Main method
- Create a SQL Connection to the testing database
- Create a SQL Command on that connection for the GetTheCurrentDate procedure
- Set the command type to stored procedure
- Open the connection
- Call the SqlCommand.ExecuteScalar() method
- Don't run it yet
- Open the stored procedure as in step # 2
- Insert a breakpoint on the line with the select statement
- Switch back the C# code
- F11 to step down to the execute scalar line
- F11 one more time
- Boom, you should be at the breakpoint in the stored procedure!
Who wants cake? Pretty wild. It should be obvious at this point all of the advantages of having the ability to debug the Transact SQL.
A lot of connections use SQL Authentication, this is fine for debugging. Only the connection created in the Server Explorer needs the Windows/Sysadmin rights. It's on this connection that the debugging is actually performed. There aren't any limitations on the connection string used in the C# code in the application.