Immediate window when debugging stored procedures in Visual Studio 2005

Debugging a saved sproc could be a threat, and I could not use the Immediate Window as I thought I would.

Is the closest window available when debugging stored procedures in VS 2005?

Is there any other way that I could run queries on the temporary table that I create inside the stored Proc? Since the scope of the Temp table is only in the stored procedure, you define it.

Can I create a debugger visualizer to query tables while I debug TSQL Stored Procs

PS . I know there is a better way to build your application so that you never have to be in this situation, but I'm looking at some kind of legacy code, so please have mercy;)

+4
source share
4 answers

I donโ€™t think Iโ€™ve ever heard of anything that allows you to debug SQL-stored procedures, for example, you can debug the "real" application code. (This applies to 7.0, 2000 and 2005, but the jury still in 2008).

When I have to do serious debugging on unfamiliar stored procedures (which may be mine a few months after I wrote them), I cut "paste the code into the SSMS request window, replace the parameters with the appropriate DECLARE and SET, comment out the problematic statements (RETURN ) and run it. In most cases, temporary tables (# temp tables, not @table variables) will still be present after it starts. Finding and replacing # with ## makes them global and accessible from other query windows, which can help. Commenting Block Saving code and running partitioned sections can be very useful.

Some other tricks I did for serious ugly procedures (hundreds or even thousands of lines of code):

Add the @Debug parameter, the default is 0. In the appropriate sections of the procedure, add the "IF @Debug = 1" blocks in which you can print the current values โ€‹โ€‹of the variables and / or the contents of the temp table. A useful form may be:

SELECT 'DebugBlock3' DataSet, * from #MyTempTable 

Based on this, another trick is to define #Temp tables as follows:

 IF @Debug = 0 or object_id('tempdb.dbo.#MyTempTable') is null CREATE TABLE #MyTempTable ( ProductId int not null ,etc ) 

In this case, if you first created the #Temp table in the query window, and then from the same window call the procedure with @Debug = 1, as soon as the procedure is completed, the temp table will still be there, any final contents were filled.

When working with dynamic code (shudder), I always get @Debug, which works with the values โ€‹โ€‹0, 1 and 2, with a comment

 -- Debug control: 0=do the work, 2=List out dynamic code, 1=Both 

And subsequent code blocks, such as:

 IF @Debug > 0 BEGIN PRINT 'Comment about the following chunk of text' PRINT '-----------------------------------------------------------' PRINT @Command END IF @Debug < 2 EXECUTE (@Command) 

Yes, this is pain, and it is not particularly convenient, but this is what I have come across over time. I honestly donโ€™t think that you can have serious debugging in SQL, like testing the current state, checking the contents of the table and, as a rule, gouging, and step-by-step code can lead to locking, deadlocks, or wildly inconsistent results if either still using the database at the same time - and I would not even like to have the remote possibility that this ever happened in any Production for which I was responsible.

+2
source

No, this functionality, unfortunately, is not implemented through the Immediate window. I do not believe that it was in any version of Visual Studio

+1
source

If your saved procs are in oracle, you can use the debugging functionality provided by oracle sql developer. This tool allows you to compile and debug your processes by setting breakpoints where necessary. Blindly go for it if the saved proc is written in pl sql. In my project, I am not debugging stored procs from Visual Studio. Instead, I get the input that is passed to the saved proc, go to this tool and run the proc process in debug mode with this input .AFAIK, you cannot query your temporary tables when debugging from VS.

0
source

I try to write stored procedures with Print instructions inside them for debugging purposes.

Then, before executing the stored procedure, I connect to the SqlConnections informational message, and then return Debug.Write to the returned message.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.infomessage%28VS.71%29.aspx

To query the temp table, you can debug their global temporary tables http://www.sqlteam.com/article/temporary-tables

This means that you can query them outside the saved process, but still recognize them as temporary tables. Global temporary tables are called ## instead of #.

0
source

All Articles