How to label result tables in multiple SELECT output

12,286

Solution 1

Add another column to the table, and name it so it will be distinguished by who reads them :)

 Select 'Employee' as TABLE_NAME, * from Employee

Output will look like this:

| TABLE_NAME | ID | Number | ...
------------------------------
| Employee   | 1  | 123    | ...

Or you can call the column 'Employee'

SELECT 'Employee' AS 'Employee', * FROM employee

The output will look like this:

| Employee | ID | Number | ...
------------------------------
| Employee | 1  | 123    | ...

Solution 2

Add an extra column, whiches name (not value!) is the label.

SELECT 'Employee' AS "Employee", e.* FROM employee e

The output will look like this:

| Employee | ID | Number | ...
------------------------------
| Employee | 1  | 123    | ...

By doing so, you will see the label, even if the result does not contain rows.

Solution 3

I like to stick a whole nother result set that looks like a label or title between the result sets with real data.

SELECT 0 AS [Our Employees:]
WHERE 1 = 0

-- Your first "Employees" query goes here

SELECT 0 AS [Our Departments:]
WHERE 1 = 0

-- Now your second real "Departments" query goes here

-- ...and so on...

Ends up looking like this:

Hacky labelled result sets in SSMS

It's a bit looser-formatted with more whitespace than I like, but is the best I've come up with so far.

Share:
12,286

Related videos on Youtube

Jude Niroshan
Author by

Jude Niroshan

Definetely not a geek. Average person who like to write clean code and share knowledge without hiding anything. #SOReadyToHelp

Updated on June 04, 2022

Comments

  • Jude Niroshan
    Jude Niroshan about 2 years

    I wrote a simple dummy procedure to check the data that saved in the database. When I run my procedure it output the data as below.

    enter image description here

    I want to label the tables. Then even a QA person can identify the data which gives as the result. How can I do it?

    **Update : ** This procedure is running manually through Management Studios. Nothing to do with my application. Because all I want to check is whether the data has inserted/updated properly.

    For better clarity, I want to show the table names above the table as a label.

  • Jude Niroshan
    Jude Niroshan about 8 years
    This is actually adding an Extra Column to the result set for each record. But still this is helpful.