How connect to two different SQL Servers in one SQL Script ?

Discussion in 'Databases' started by albanello, Feb 21, 2014.

  1. Hi
    I would like to be able to work with TWO different SQL Servers and Database in ONE SQL script.
    ie:
    - Transfer data to/from MyDASPServer_Database from/to MyLocalServer_Datebase
    - Work on MyDASPServer_Database while also working on MyLocalServer_Datebase in the same SQL script
    I know I can do this with a SSIS dtsx package but I would like to be able to do it in a Script also.
    If this is possible could someone please give me the syntax, to connect to two differernt SQL 2008 R2 servers, or a link that explains how to do this.

    SQL Server 2008 R2 at Discount ASP.NET
    Thanks in advance
    FrankA
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    You should take a look at this link. In order to work with different databases on different servers, you need to specify the four-part object qualifier:

    server.database.schema.object

    For example:

    [sql2k801.discountasp.net].[mydb_name].[dbo].[customer_table]

    You also need to setup the DiscountASP.NET database as linked server in your local SQL Server Instance. You can only setup a linked server from your local SQL Server instance. It doesn't work the other way around (i.e. we don't allow linked servers from the DiscountASP.NET database servers).
     
  3. RayH
    Thank You for your response.
    I think I got it working with your help. When I am connected to my Local Server and I execute the following "SELECT" I get the response I expect from my DASP Database.

    SELECT Top(10) * FROM [MySQLServer??????.discountasp.net].[SQL??????_??????_MyDASPDatabase].[dbo].[MyTable????????]
    Note: I have hidden some character for security, but I think you get the idea.
    Took a little time to get the Linked server set up but I think it is working.
    ONE more question:
    ????????
    When I use my local server now, does the Linked Server cause me to be connected to my DASP server even when I am not using the DASP Server OR does the Link Server only active when the "FROM [MySQLServer??????.discountasp.net].[SQL??????_??????_MyDASPDatabase].[dbo].[MyTable????????]" is used on a TSQL command
    ????????
    Thanks again
    Frank A
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

  5. mjp

    mjp

    @dmitri probably knows the answer. Maybe next time he checks in he will chime in.
     

Share This Page