table name as part of the results?

Posted on 2012-08-10
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.
Question by:knowlton
    LVL 65

    Accepted Solution

    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.
    LVL 5

    Author Comment

    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"
    LVL 68

    Assisted Solution

    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).
    LVL 18

    Assisted Solution

    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.
    LVL 5

    Author Closing Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now