locked
Executing the SQL query across 2 different databases RRS feed

  • Question

  • Hello All,

    I know its a very beginner question but still felt to get it confirmed.

    Is it possible some how to execute a JOIN SQL Query across 2 different databases of a SQL Server

    I have even tried that but was not able to do so, It wept saying object not found (and rightly so). But still is there a tweak/hack or something which can make this happen.
    • Edited by paragkalra Monday, January 25, 2010 8:16 AM
    Monday, January 25, 2010 7:23 AM

Answers

  • To refer the  table in other database, use the following convention

    DataBaseName.SchemaName.TableName
    • Proposed as answer by Arif Hasan Monday, January 25, 2010 7:55 AM
    • Marked as answer by Zongqing Li Friday, January 29, 2010 7:34 AM
    Monday, January 25, 2010 7:25 AM

All replies

  • To refer the  table in other database, use the following convention

    DataBaseName.SchemaName.TableName
    • Proposed as answer by Arif Hasan Monday, January 25, 2010 7:55 AM
    • Marked as answer by Zongqing Li Friday, January 29, 2010 7:34 AM
    Monday, January 25, 2010 7:25 AM
  • ---If both the datbases are in the same server

    select

     

    *
    from DatabaseName.dbo.TableName AS A INNER JOIN DatabaseName.dbo.TableName AS B
    ON A.CommanColumnName=B.CommanColumnName
    ---If one the datbases is in the other server
    select *
    from DatabaseName.dbo.TableName AS A INNER JOIN LinkedServerName.DatabaseName.dbo.TableName AS B
    ON A.CommanColumnName=B.CommanColumnName

    Thanks
    Ramesh.M

    Monday, January 25, 2010 7:28 AM