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
 General SQL Server Forums
 New to SQL Server Programming
 How to select data from different databases

Author  Topic 

joelseverich
Starting Member

34 Posts

Posted - 2008-01-10 : 11:17:00
Hi I'm trying to select data from different databases, Actually databases have the same structure and tables but different data, so I want to mix data from specific tables of all databases.
Any help would be great

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-10 : 11:21:58
Are all the databases in same server? If yes, use:-
SELECT field1,field2,... FROM DB1.<schema>.Table
UNION ALL
SELECT field1,field2,... FROM DB2.<schema>.Table
UNION ALL
SELECT field1,field2,... FROM DB3.<schema>.Table
.....
Go to Top of Page

joelseverich
Starting Member

34 Posts

Posted - 2008-01-10 : 11:51:08
I tried this but it says "DB2.... is not a valid object"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-10 : 11:53:34
the format is <dbname>.<schemaname>.<tablename>

so if your db=DB1
schema=sc1
tablename =t1
then
SELECT * FROM DB1.sc1.t1
Go to Top of Page

joelseverich
Starting Member

34 Posts

Posted - 2008-01-10 : 12:02:18
I changed some parameters and it worked thank you...
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2008-01-10 : 13:46:07
CREATE it as a VIEW

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -