Search code examples
sqlt-sqlsql-server-2000

searching data from multiple database


Good Afternoon in GMT +8

is there a way to search for a data from multiple databases? uhm, i know that we could do something like below:

select * from table1 where [name] = 'John'

select * from table2 where [name] = 'John'

is there a way to search for it like

select * from sometables where [name] = 'john'


Solution

  • Try this...

      SELECT * FROM [DataBaseName].[Schema].[Table] WHERE [Your Condition]
    

    You can also use such queries in Join's e.g You have two DataBases

    DataBase1: 
    DataBase Name : MyDB1
    Table Name : Table1
    DataBase2:
    DataBase Name : MyDB2
    Table Name : Table1
    

    Note: Both tables have same structure like (Id, Name, FatherName, Address)

    Your query will be ..

    SELECT      *
    FROM        MyDB1.dbo.Table1 a
    INNER JOIN  MyDB2.dbo.Table1 b
    ON          a.Id = b.Id