Solved

Include table name in query results

Posted on 2008-10-31
3
437 Views
Last Modified: 2012-05-05
I have several tables that are all filled with basically the same information but each for a different school.  I need to pull a few pieces of information from each so that I can do some comparison analysis.  The problem is that I need to know which table each result was pulled from so that I know which school it pertains to.  Is there any way to include the actual table name in the results of the query?
0
Comment
Question by:Ima Jedi
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 22850395
Like this?

SELECT 'table1' AS Source, Col1, Col2, Col3
FROM table1
UNION ALL
SELECT 'table2' AS Source, Col1, Col2, Col3
FROM table2
UNION ALL
SELECT 'table3' AS Source, Col1, Col2, Col3
FROM table3
0
 
LVL 5

Expert Comment

by:JBart_17
ID: 22850415
select 'School1',fieldA, fieldB from table1
select 'School2',fieldA, fieldB from table2
hardcode it in
0
 

Author Closing Comment

by:Ima Jedi
ID: 31512050
Thank you Patrick!  That works perfectly.  It figures that it would be such a simple solution.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql ide 10 39
Insert values are dynamic 11 41
SQL Server 2012 r2 - Make Temp Table Query Faster 5 40
SQL Server - Find Parent/Child relationship tree between numeric range. 7 41
This article describes some very basic things about SQL Server filegroups.
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

786 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