INSERT INTO from two different server database

后端 未结 5 1996
走了就别回头了
走了就别回头了 2021-02-02 10:07

I am trying to copy the data of testdabse.invoice table to basecampdev.invoice table. testdabse is a local database while basecampde

5条回答
  •  时光说笑
    2021-02-02 10:48

    It sounds like you might need to create and query linked database servers in SQL Server

    At the moment you've created a query that's going between different databases using a 3 part name mydatabase.dbo.mytable but you need to go up a level and use a 4 part name myserver.mydatabase.dbo.mytable, see this post on four part naming for more info

    edit
    The four part naming for your existing query would be as shown below (which I suspect you may have already tried?), but this assumes you can "get to" the remote database with the four part name, you might need to edit your host file / register the server or otherwise identify where to find database.windows.net.

    INSERT INTO [DATABASE.WINDOWS.NET].[basecampdev].[dbo].[invoice]
           ([InvoiceNumber]
           ,[TotalAmount]
           ,[IsActive]
           ,[CreatedBy]
           ,[UpdatedBy]
           ,[CreatedDate]
           ,[UpdatedDate]
           ,[Remarks])
    SELECT [InvoiceNumber]
           ,[TotalAmount]
           ,[IsActive]
           ,[CreatedBy]
           ,[UpdatedBy]
           ,[CreatedDate]
           ,[UpdatedDate]
           ,[Remarks] FROM [BC1-PC].[testdabse].[dbo].[invoice]
    

    If you can't access the remote server then see if you can create a linked database server:

    EXEC sp_addlinkedserver [database.windows.net];
    GO
    USE tempdb;
    GO
    CREATE SYNONYM MyInvoice FOR 
        [database.windows.net].basecampdev.dbo.invoice;
    GO
    

    Then you can just query against MyEmployee without needing the full four part name

提交回复
热议问题