INSERT INTO from two different server database

后端 未结 5 1873
走了就别回头了
走了就别回头了 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条回答
  •  猫巷女王i
    2021-02-02 10:37

    The answer given by Simon works fine for me but you have to do it in the right sequence: First you have to be in the server that you want to insert data into which is [DATABASE.WINDOWS.NET].[basecampdev] in your case.

    You can try to see if you can select some data out of the Invoice table to make sure you have access.

    Select top 10 * from [DATABASE.WINDOWS.NET].[basecampdev].[dbo].[invoice]
    

    Secondly, execute the query given by Simon in order to link to a different server. This time use the other server:

    EXEC sp_addlinkedserver [BC1-PC]; -- this will create a link tempdb that you can access from where you are
    GO
    USE tempdb;
    GO
    CREATE SYNONYM MyInvoice FOR 
        [BC1-PC].testdabse.dbo.invoice; -- Make a copy of the table and data that you can use
    GO
    

    Now just do your insert statement.

    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 MyInvoice
    

    Hope this helps!

提交回复
热议问题