We help IT Professionals succeed at work.

table name as part of the results?

Medium Priority
476 Views
Last Modified: 2012-08-10
Is it possible to show the name of the table in the results window?

For example:

select '' as tbmBatchParameter, * from tbmBatchParameter where project_id = @ProjectID

shows this in the Results window:

show table name in results
but this is kind of a hack.  

Is there a cleaner way to do it?


This isn't a problem with just one or two selects, but with 20 selects being displayed it can be confusing which table you are looking at.
Comment
Watch Question

SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
select 'tbmBatchParameter ' as table_name, *
from tbmBatchParameter
where project_id = @ProjectID

Works great when you're doing a UNION, or viewing multiple sets in SSMS.
Tom KnowltonWeb developer

Author

Commented:
Is there any way to "get" the table name programmatically?

select '' + system.GetTableName( ) ' as system.GetTableName( ), *
from tbmBatchParameter
where project+id = @ProjectID


system.GetTableName( ) would return a string:   "tbmBatchParameter"
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
Sadly there is no generic function doing that for you, so you have to integrate it manually, either has column named like the table (as you did), or as a new column containing the table name (as jimhorn posted).
CERTIFIED EXPERT
Top Expert 2011
Commented:
You are setting tbmBatchParameter in the from clause anyway , just add it to the select clause too. You can go and get the name of the table form the system tables , but it will be a problem to connect it to the tables used in the statement.
Tom KnowltonWeb developer

Author

Commented:
Thanks!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.