Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Join 2 tables with different column headings

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-25 : 08:36:34
Tumbleweed3079 writes "In SQL can I join 2 tables with the same information into a view?

I have a USPEOPLE table that contains name, phone etc...
I also have a UKPEOPLE table that contains the same data but in a different column heading....field1, field2, field3...etc.

Can I join these in a view with custom headings? I also need to keep them in different tables because UKPEOPLE is updated by users and USPEOPLE is a DTS import that happens daily.

Any suggestions?"

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-03-25 : 09:22:25
If I follow, you're asking if you can UNION select statements together which reference different column names.

The answer is yes - the only two requirements for a UNION is (1) the # of columns is the same, and (2) the data types are the same.

setBasedIsTheTruepath
<O>
Go to Top of Page

davidpardoe
Constraint Violating Yak Guru

324 Posts

Posted - 2002-03-25 : 09:56:02
Just to add to "set's" answer - they don't even need to be the same data type so long as an implicit conversion can be carried out.

Also, the column names from the first part of a UNION statement will be used to the name the columns in the result set. So, if USPEOPLE has sensible column names and UKPEOPLE does not then do the SELECT from USPEOPLE in the first part:-

SELECT ... FROM USPEOPLE
UNION
SELECT ... FROM UKPEOPLE




============================
Chairman of
The NULL Appreciation Society
"Keep NULLs as NULL"
Go to Top of Page
   

- Advertisement -