dbTalk Databases Forums  

Union 2 databases

microsoft.public.sqlserver.setup microsoft.public.sqlserver.setup


Discuss Union 2 databases in the microsoft.public.sqlserver.setup forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Lousada
 
Posts: n/a

Default Union 2 databases - 05-05-2010 , 08:05 AM






Hello! I have 2 databases (DBa and DBb),

dba Name,Age and dba Name,Sex i want to join to get name,age
and sex?

how do i get this data?

Reply With Quote
  #2  
Old   
Rick Byham, MSFT
 
Posts: n/a

Default Re: Union 2 databases - 05-05-2010 , 10:59 AM






Assuming both databases are on the same instance of SQL Server.
Assuming the data is in Table1 and in Table2 and both tables are in the dbo
schema.
Assuming the Name field is a unique identifier so you won't have two people
named John Smith.
And assuming you have permission to access the databases and tables, then
try a query like:

SELECT DBa.Table1.dbo.Name, DBb.Table2.dbo.Name, Age, Sex
FROM DBa..dbo.Table1
JOIN DBb.dbo.Table2
ON DBa.dbo.Table1.Name = DBb.dbo.Table2.Name

I have intentionally included the Name column from both tables to emphasis
that you could use either one.
Since it is possible to have two people with the same name, I suggest adding
an additional column to uniquely identify each person. Maybe PersonID. Then
the ON clause would be ON DBa.dbo.Table1.PersonID= DBb.dbo.Table2.PersonID

If you really meant that you had two tables (instead of two databases), then
you wanted:
SELECT DBa.Name, DBb.Name, Age, Sex
FROM DBa
JOIN DBb
ON DBa.Name = DBb.Name
--
Rick Byham, MSFT
(Implies no warranty or rights)


"Lousada" <carloslousada (AT) gmail (DOT) com> wrote

Quote:
Hello! I have 2 databases (DBa and DBb),

dba Name,Age and dba Name,Sex i want to join to get name,age
and sex?

how do i get this data?

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.