LInk two tables from two databases

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imtmub
    New Member
    • Nov 2006
    • 112

    LInk two tables from two databases

    Hi,
    I have two different databases in our company for diffrent departments. for some reason i need to connect few tables to another database.So i created tables in the other database which i need to link. If anybody know to link the table i m very greatful.
    Thanks
    imt
  • almaz
    Recognized Expert New Member
    • Dec 2006
    • 168

    #2
    Originally posted by imtmub
    Hi,
    I have two different databases in our company for diffrent departments. for some reason i need to connect few tables to another database.So i created tables in the other database which i need to link. If anybody know to link the table i m very greatful.
    Thanks
    imt
    Are these databases reside on the same SQL Server? If yes, than you don't have to "link" tables, just use them :). For example, you have a table1 in database1 and you want to join it to table2 in database2. Than you can use the following script:
    Code:
    select * 
    from table1 t1 inner join database2..table2 t2 on t1.ID = t2.ID

    Comment

    • iburyak
      Recognized Expert Top Contributor
      • Nov 2006
      • 1016

      #3
      Just in case two servers are not linked to each other but you still have access to another server you can try query below:

      [PHP]select *
      from table1 t1
      inner join OPENDATASOURCE(
      'SQLOLEDB',
      'Data Source=server_n ame;User ID=UserID;Passw ord=password'
      ).database_name .dbo.table2 t2 on t1.ID = t2.ID[/PHP]

      Comment

      • imtmub
        New Member
        • Nov 2006
        • 112

        #4
        Originally posted by iburyak
        Just in case two servers are not linked to each other but you still have access to another server you can try query below:

        [PHP]select *
        from table1 t1
        inner join OPENDATASOURCE(
        'SQLOLEDB',
        'Data Source=server_n ame;User ID=UserID;Passw ord=password'
        ).database_name .dbo.table2 t2 on t1.ID = t2.ID[/PHP]
        Thanks i have two diffrent server, i hope the second one will work.

        Comment

        • almaz
          Recognized Expert New Member
          • Dec 2006
          • 168

          #5
          Originally posted by imtmub
          Thanks i have two diffrent server, i hope the second one will work.
          If you want to link servers on a permanent basis, see linked servers

          Comment

          • imtmub
            New Member
            • Nov 2006
            • 112

            #6
            Originally posted by almaz
            If you want to link servers on a permanent basis, see linked servers
            Hi,
            in my tables t1.ID = t2.ID is not matching. bcz t2.id is new table. if anybody enter the data in table 1 in real time need to copy in table2. Not all the fields but few fields i need to copy.

            Thanks
            imt

            Comment

            • iburyak
              Recognized Expert Top Contributor
              • Nov 2006
              • 1016

              #7
              Originally posted by almaz
              If you want to link servers on a permanent basis, see linked servers
              By the way for security and other reasons it is not allowed to link servers in my company.

              Comment

              • Pratibha Karpe
                New Member
                • Jun 2014
                • 1

                #8
                It works only when you refer table1 as database1..tabl e1

                Comment

                Working...