Can we see the values (rows and cells) in a table valued variable in SQL Server Management Studio (SSMS) during debug time? If yes, how?
9 Answers
DECLARE @v XML = (SELECT * FROM <tablename> FOR XML AUTO)
Insert the above statement at the point where you want to view the table's contents. The table's contents will be rendered as XML in the locals window, or you can add @v
to the watches window.
-
1This is definitely a work around that is good enough for small tables to be presented as XML.– FaizFeb 16, 2015 at 12:38
-
4But still don't stop reading! Another awesome option below! Great thread!!– Mike MMay 25, 2015 at 12:33
-
1I have had large tables with the XML not very friendly to read. I take another step - copy the XML and paste in xmlgrid.net and you can see the XML as a table. The table visualization really helps. Waiting for a release of SSMS that has a table viewer like the datatable viewer in Visual Studio. Feb 21, 2017 at 1:19
-
1You can add ",ROOT('rootNodeName')" to the "FOR XML" clause. This will collect multiple rows (if any) under a single root, which makes for a legal XML document, which can be viewed with the XML visualizer instead of the text visualizer.– JohnL4Sep 25, 2017 at 18:53
-
4Using SQL Server 2016 or never you can also use JSON version of this
DECLARE @v nvarchar(max) = (SELECT * FROM <tablename> FOR JSON AUTO)
– SousukeDec 21, 2018 at 12:16
That's not yet implemented according this Microsoft Connect link: Microsoft Connect
-
4
-
2And here we are in 2016. No better. The Connect link also doesn't work anymore.– dotNETApr 27, 2016 at 3:50
-
6To be honest this is not actually an answer as it does not answer the question, the question is how to do it (not whether SSMS has any feature for it), stating this is not implemented while there are ways to display the values is not very helpful. Nov 21, 2016 at 11:15
-
2Please delete this answer. The answer below should be the accepted answer. Aug 3, 2017 at 8:07
-
This project https://github.com/FilipDeVos/sp_select has a stored procedure sp_select
which allows for selecting from a temp table.
Usage:
exec sp_select 'tempDb..#myTempTable'
While debugging a stored procedure you can open a new tab and run this command to see the contents of the temp table.
-
7
-
Also hangs if there is an open transaction, so not much use if using a debugging a test in a framework such as tSQLt which always opens a transaction at the start of the test.– NathanJan 24, 2018 at 21:37
-
1
-
Good - would be great if you could sort by a single column or two Dec 12, 2019 at 11:56
-
In the Stored Procedure create a global temporary table ##temptable and write an insert query within your stored procedure which inserts the data in your table into this temporary table.
Once this is done you can check the content of the temporary table by opening a new query window. Just use "select * from ##temptable"
If you are using SQL Server 2016 or newer, you can also select it as JSON result and display it in JSON Visualizer, it's much easier to read it than in XML and allows you to filter results.
DECLARE @v nvarchar(max) = (SELECT * FROM Suppliers FOR JSON AUTO)
-
For people (like me) who are not familiar with JSON Visualizers: Next to the variable's 'Value' in the Locals or Watch window there's a pulldown. Select JSON to get the window shown here. May 30, 2021 at 21:11
I have come to the conclusion that this is not possible without any plugins.
-
I saw some previews of Visual Studio 2010. Very fancy, complex, and detailed... and the demonstrator did not know if temp tables could be viewed in debug mode. Maybe when it's released, but I'm not holding my breath. Jan 6, 2010 at 17:25
SQL Server Profiler 2014 lists the content of table value parameter. Might work in previous versions too. Enable SP:Starting or RPC:Completed event in Stored Procedures group and TextData column and when you click on entry in log you'll have the insert statements for table variable. You can then copy the text and run in Management Studio.
Sample output:
declare @p1 dbo.TableType
insert into @p1 values(N'A',N'B')
insert into @p1 values(N'C',N'D')
exec uspWhatever @PARAM=@p1
Why not just select the Table and view the variable that way?
SELECT * FROM @d
-
5Please explain how to go about that. Adding a
SELECT * FROM @Table
to the script being debugged does not emit results to the results window. Apr 10, 2018 at 17:32
Sorry guys, I'm a little late to the party but for anyone that stumbles across this question at a later date, I've found the easiest way to do this in a stored procedure is to:
- Create a new query with any procedure parameters declared and initialised at the top.
- Paste in the body of your procedure.
- Add a good old fashioned select query immediately after your table variable is initialised with data.
- If 3. is not the last statement in the procedure, set a breakpoint on the same line, start debugging and continue straight to your breakpoint.
- Profit!!
messi19's answer should be the accepted one IMHO, since it is simpler than mine and does the job most of the time, but if you're like me and have a table variable inside a loop that you want to inspect, this does the job nicely without too much effort or external SSMS plugins.