×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Union two tables only if one table has data
2

Union two tables only if one table has data

Union two tables only if one table has data

(OP)
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.

RE: Union two tables only if one table has data

CODE

SELECT * FROM TableA
UNION ALL
SELECT * FROM TableB
WHERE (SELECT COUNT(*) FROM TableA) > 0 

Borislav Borissov
VFP9 SP2, SQL Server

RE: Union two tables only if one table has data

(OP)
bborissov, to take it another step further how would I only pull from Table B only if Column A is found in Table A.

RE: Union two tables only if one table has data

smile

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

RE: Union two tables only if one table has data

You may also look at these Visual Representation of SQL Joins

Quote (from this site)

This article describes SQL Joins in a visual manner, and also the most efficient way to write the visualized Joins.


---- Andy

There is a great need for a sarcasm font.

RE: Union two tables only if one table has data

(OP)
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. :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close