views:

453

answers:

5

Debugging stored sproc can be menace, & I was not able to use the Immediate Window like the way I thought I would be.

Is the immediate window available while debugging Stored Procs in VS 2005?

Is there any other way I would be able to run queries on a Temporary Table which I am creating inside a Stored Proc ? As the scope of the Temp Table is only in the Stored procedure you define it.

Can I create a Debugger Visualizer to query the Tables whilst I'm debugging a TSQL Stored Procs

PS: I know there are better way to arch your application so that you never have to be in this situation, but I'm looking at some legacy code so please have some mercy ;)

A: 

Having the same issue.

Anyone? Bueller?

A: 

If your stored procs are in oracle, you can use the debugging functionality provided by oracle sql developer. This tool provides you to compile and debug your procs by setting break points where ever needed. Blindly go for this if the stored proc is written in pl sql. In my project, i dont debug stored procs from Visual Studio. Instead i get hold of the input which is passed to the stored proc, go to that tool and run the proc in debug mode with that input.AFAIK, you cannot query your temp tables while debugging from VS.

Cshah
+2  A: 

I don't think I've ever heard of anything that lets you debug SQL stored procedures like you can debug "real" application code. (This covers 7.0, 2000, and 2005, but the jury's still out on 2008).

When I have to do serious debugging on unfamiliar stored procedures, (which could be mine, months after I wrote them), I cut 'n paste the code to an SSMS query window, replace the parameters with appropriate DECLAREs and SETs, comment out problematic statements (RETURN), and run it. In most cases, the temp tables (# temp tables, not @ table variables) will still be there after it runs. Searching and replacing on # with ## makes them global and accesable from other query windows, which can help. Commenting out blocks of code and running the uncommented sections can be very useful.

Some other tricks I've done for seriously ugly procedures (hundreds, or even thousands of lines of code):

Add parameter @Debug, defaulting to 0. In pertinent sections of the procedure, add "IF @Debug = 1" blocks, within which you can print out current variable values and/or temp table contents. A useful form can be:

SELECT 'DebugBlock3' DataSet, * from #MyTempTable

Building on this, another trick is to define #Temp tables like so:

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

With this, if you first CREATE the #Temp table in a query window, and then from the same window call the procedure with @Debug = 1, once the procedure is completed the temp table will still be there, populated with whatever the final contents were.

When working with dynamic code (shudder), I've invariable got @Debug set to work with values 0, 1, and 2, with the comment

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

And subsequent code blocks like:

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

Yes, this is a pain and not particularly convenient, but it's what I've come up with over time. I honestly don't think you can have serious debugging in SQL, as testing current state, checking table contents, and generally poking around while stepping through code could result in blocking, deadlocks, or wildly inconsistant results if anyone else is using the database at the same time -- and I'd hate to have even the remote possibility of this ever happening on any Production that I was responsible for.

Philip Kelley
Visual Studio does let you debug stored procedures (almost) like "real" code - http://msdn.microsoft.com/en-us/library/zefbf0t6%28VS.71%29.aspx
AB Kolan
Fair enough, though (as per your link) it seems a bit awkward to set up. If you can't access temp table data during the step-through, work arounds like these would seem to be necessary.
Philip Kelley
A: 

I tend to write stored procedures with Print statements within them for debugging purposes.

Then before I execute the stored procedure I hook into the SqlConnections info message, then do Debug.Write on the message returned.

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

For the temp table querying you could whilst debugging make them global temp tables http://www.sqlteam.com/article/temporary-tables

This means you can query them outside of the stored proc but still have them recognized as temp tables. Global temp tables are named ## instead of just #.

Peter
+1  A: 

No this functionality is unfortunately not implemented via the Immediate Window. I don't believe it has been in any version of Visual Studio

JaredPar