Solved

Get the table name in Union Select too?

Posted on 2014-04-28
4
518 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 400 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 100 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

726 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