Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Union two tables only if one table has data 2

Status
Not open for further replies.

1tufgt

Programmer
Apr 11, 2020
34
US
What would be the best process to union two tables together based on if Table A has data I would only pull data from Table B?

SELECT Column A, Column B, Column C FROM Table A
union
Select Column A, Column B, Column C FROM Table B

So in the end I need to union two tables but only pull data if data is returned from Table A.
 
Code:
SELECT * FROM TableA
UNION ALL
SELECT * FROM TableB
WHERE (SELECT COUNT(*) FROM TableA) > 0

Borislav Borissov
VFP9 SP2, SQL Server
 
bborissov, to take it another step further how would I only pull from Table B only if Column A is found in Table A.
 
:)
Code:
Select Column A, Column B, Column C FROM Table B
WHERE EXISTS(SELECT * FROM INFORMATION_SCHEMA.Columns WHERE Table_Name = 'TableA' AND Column_Name = 'ColumnA')

Borislav Borissov
VFP9 SP2, SQL Server
 
bborissov, thanks that's what I was looking for.

Andrzejek, thanks for the info on the joins. I already have a printout of those joins and was struggling with only selecting after a initial select. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top