Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view data in table variables during debugging session in MS SQL Management Studio 2012?

Tags:

I would like to debug a complex T-SQL script using SSMS 2012.

I can run the script in debug mode and place breakpoints, as well as step through my script, but I can't see the values stored in my table variables.

In the Locals window I see all these variables, but their value is shown as (table):

Locals window

There is no way to view the content of the variable through the context menu or by clicking on the variable.

I tried to use the Immediate Window to run a query on the table variable, but this seems not to work either.

Immediate Window

Any idea how I can get the values from my table variables in the debug session?

like image 846
Alexander Galkin Avatar asked Jul 12 '13 08:07

Alexander Galkin


2 Answers

Whilst I can't find any documetation, anywhere, that explicitly states that you cannot inspect table variables, I don't believe that it's possible. From Transact-SQL Debugger

Locals and Watch. These windows display currently allocated Transact-SQL expressions. Expressions are Transact-SQL clauses that evaluate to a single, scalar expression. The Transact-SQL debugger supports viewing expressions that reference Transact-SQL variables, parameters, or the built-in functions that have names that start with @@. These windows also display the data values that are currently assigned to the expressions.

(My emphasis)

That is, you can only inspect scalars.

As to your attempt to use the Immediate window, the Limitations on Debugger Command and Features says:

The Immediate window is displayed, but you cannot do anything useful with it, such as setting a variable to a value, or querying the database.


I've never really used the debugger much - everytime I've looked into it, I encounter limitations like this.

That's why I still tend to use "old-skool"/"printf" approaches to debug SQL - include extra SELECT *s liberally throughout the code showing the current state of tables, and extra PRINT or RAISERROR messages that show other states, etc. And then just run the code normally, until you've bashed it into shape.

like image 120
Damien_The_Unbeliever Avatar answered Sep 21 '22 15:09

Damien_The_Unbeliever


Using the next code you can see the content of your table as XML.

DECLARE @v XML = (SELECT * FROM <tablename> FOR XML AUTO)

It is useful to check what your SELECT statements return. I tested it and it works.

Read more here.

like image 22
Mario Kutlev Avatar answered Sep 20 '22 15:09

Mario Kutlev