[ Team LiB ] Recipe 9.8 Debugging aSQLServerStoredProcedure Problem Given an application that uses a SQLServerstoredprocedure that is causing errors, you need to debug the stored procedure. Solution Use Visual Studio .NET to debug SQLServerstored procedures (in both standalone mode and from managed code). Discussion Debuggingastoredprocedure in standalone mode You can debug astoredprocedure in standalone mode from Visual Studio .NET Server Explorer by following these steps: 1. Open the Server Explorer window in Visual Studio .NET by selecting it from the View menu. 2. Create a connection to the database or select an existing connection. 3. Select and expand the node for the database that contains the stored procedure. 4. Expand the Stored Procedures node. 5. Right-click on the storedprocedure to be debugged and select Step Into StoredProcedure from the popup menu. 6. If requested, supply the parameter values on the Run StoredProcedure dialog. Alternatively, if the storedprocedure is already open in a source window in Visual Studio .NET: 1. Right-click on the storedprocedure to be debugged and select Step Into StoredProcedure from the popup menu. 2. If requested, supply the parameter values on the Run StoredProcedure dialog. Debugging astoredprocedure from managed code To debug astoredprocedure from managed code, SQLdebugging must be enabled for the project. Follow these steps: 1. Open the solution. 2. In the Solution Explorer window, select the project and right-click. Select Properties from the popup menu. 3. In the Property Pages dialog, select Debug from the Configuration drop-down list box. 4. Select the Configuration Properties folder in the left pane and choose Debugging. 5. In the Debuggers section of the right pane, set Enable SQLDebugging to true. 6. Click OK to close the dialog. Table 9-2 lists the components that must be installed for SQLServer debugging. Table 9-2. SQLServerdebugging components Component Installation location SQLLE.DLL Client SQLDBG.DLL Client and server MSSDBI98.DLL Server in the \binn directory of the SQLServer instance SQLDBREG2.EXE Client There are some other significant limitations to SQLServer Debugging: • It is not possible to debug SQL statements that are outside of astored procedure. • It is not possible to step into astoredprocedure from managed or unmanaged code, or into managed or unmanaged code from astored procedure. Set a breakpoint at entry point in the storedprocedure or in the reentry point in the code as required. Alternatively, open the code or storedprocedure and right-click on the line to break on. Select Run to Cursor from the shortcut menu to reach the desired line without setting a breakpoint. • The database connection from your application must be established with the .NET data provider for SQLServer before debugginga mixed-language application. After that, you can open stored procedures and set breakpoints in the same way as for other applications. • When connection pooling is enabled, debuggingastoredprocedure called from native or managed code might not work after the first time. When a connection is obtained from the pool rather than created, SQLdebugging is not reestablished. • Changes to locals or parameter variables that are cached by the SQL interpreter are not automatically modified and there is no way to force the cache to refresh. SQLServer caches variables when the execution plan determines that they will not be dynamically loaded for each statement execution or reference. For more information about debugging SQLstored procedures, see the topic "Debugging SQL" in the MSDN Library. [ Team LiB ] . SQL Server before debugging a mixed-language application. After that, you can open stored procedures and set breakpoints in the same way as for other applications into a stored procedure from managed or unmanaged code, or into managed or unmanaged code from a stored procedure. Set a breakpoint at entry point in the stored