How to select different databases

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    How to select different databases

    Hi,
    I have two different severs
    154.125.1.3
    where I have a database called demo which consists of table t1
    Now another server
    102.154.3.1
    where i have a database called demo1 which consists of table t2

    Now I want to write a query where I want to join both these tables.
    Any idea please

    Regards
    cmrhema
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    For that you will have to link the second server to your first current server using linked servers option in enterprise manager or using the stored procedure called sp_addlinkedser ver.

    Then in your query you can use tables of the remote server by qualifying the table name with the linked server name.

    For Example:

    select * from linkedServerNam e.tablename

    where linkedServerNam e=3Dname of the linked server defined while linking the server tablename=3Dnam e of table on the remote server

    You can use sp_addlinkedser ver "server name" to create a linked server. You can link SQL Server to other data sources also.

    sp_linkedserver s will give you a listing of all the linked servers on your SQL Server.

    To run a query over a linked server

    select * from [server name].[database name].[owner name].[table name]

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      [code=sql]
      select <columns>
      from <Servername1>_. <Database>.dbo. <Table> as b join <servername2>_. <Database>.dbo. <table> as a
      on a.<Column> = b.<Column>
      where a.<Column> <> b.<Column>
      order by a.<Column>
      [/code]

      Comment

      Working...