?
Solved

Get the table name in Union Select too?

Posted on 2014-04-28
4
Medium Priority
?
549 Views
Last Modified: 2014-04-28
Hello,

I am trying to query several tables that have the same 3 columns and combine the results like this.

Select col1,col2,col3 from tblA
union
Select col1,col2,col3 from tblB
union
Select col1,col2,col3, from tblC

From
The end result who not only show me the union of three columns form all the tables but also - which is there source table as well like this

Col1 | col2 | col3 | myTableName
Er | ty | rt } tblA
Er | ty | rt } tblB
0
Comment
Question by:Rayne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
plusone3055 earned 1600 total points
ID: 40027579
yes
Select col1,col2,col3, Tbla as tablename  from tblA
union all
Select col1,col2,col3, tblb as tablename from tblB
union all
Select col1,col2,col3, tblc as tablename from tblC
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 total points
ID: 40027580
Select col1,col2,col3, 'tblA' AS table_name from tblA
union all
Select col1,col2,col3, 'tblB' from tblB
union all
Select col1,col2,col3, 'tblC' from tblC
order by col1, col2, col3
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40027583
hehehe I won by 11 seconds LOL
0
 

Author Closing Comment

by:Rayne
ID: 40027828
thank you both :)
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
New style of hardware planning for Microsoft Exchange server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

762 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