Click to See Complete Forum and Search --> : Select Query from multiple databases


Pbear74
11-16-2009, 03:24 PM
I have 7 databases that the same in structure. There is a users table in each database and what i need to do is get a count of all the distinct users from those 7 databases.
It's simple enough to do it in one db:
SELECT DISTINCT(loginID) FROM UsersTable
but how to get the other tables from other databases into the mix?


This is MS SQL 2005 by the way.

Thanks in advance.

ssystems
11-16-2009, 05:24 PM
From master you can call each table by

database.schema.table



SELECT distinct [columnName] FROM (
SELECT [columnList] FROM dbname1.schema1.table1
UNION SELECT [columnList] FROM dbname2.schema2.table2
.....
)