[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

table name as part of the results?

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.
Tom Knowlton
Tom Knowlton
3 Solutions
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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 developerAuthor 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"
QlemoC++ DeveloperCommented:
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).
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 developerAuthor Commented:

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now