28

我正在尝试将表的数据复制testdabse.invoicebasecampdev.invoice表中。 testdabse是本地数据库,而basecampdev在服务器中。

我将数据复制到另一个表的查询不起作用,它说

Invalid object name 'basecampdev.dbo.invoice'.  

我一直在阅读此文档,但发现很难理解和理解。

这些是服务器提供的信息

Server type: Database Engine
Server name: server.database.windows.net (this is not the real name)
Authentication: SQL Server Authentication
Login: myusername
Password: mypassword  

如何连接到服务器以便能够运行此查询

INSERT INTO [basecampdev].[dbo].[invoice]
           ([InvoiceNumber]
           ,[TotalAmount]
           ,[IsActive]
           ,[CreatedBy]
           ,[UpdatedBy]
           ,[CreatedDate]
           ,[UpdatedDate]
           ,[Remarks])
SELECT [InvoiceNumber]
           ,[TotalAmount]
           ,[IsActive]
           ,[CreatedBy]
           ,[UpdatedBy]
           ,[CreatedDate]
           ,[UpdatedDate]
           ,[Remarks] FROM [testdabse].[dbo].[invoice]

截屏

在此处输入图像描述

4

5 回答 5

26

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

于 2013-01-04T08:43:48.630 回答
5

西蒙给出的答案对我来说很好,但你必须按照正确的顺序来做:首先你必须在你想要插入数据的服务器中,你的 [DATABASE.WINDOWS.NET].[basecampdev]案子。

您可以尝试查看是否可以从 Invoice 表中选择一些数据,以确保您可以访问。

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

其次,执行 Simon 给出的查询以链接到不同的服务器。这次使用其他服务器:

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

现在只需执行您的插入语句。

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

希望这可以帮助!

于 2015-01-08T22:44:20.637 回答
4

如果源数据库不在链接服务器中,则不能直接将表从不同的数据库复制到目标服务器数据库中。但是一种可能的方法是,将所需表的脚本(带有数据的模式)临时生成到源服务器数据库中的一个表中,然后在目标服务器数据库中执行脚本以使用您的数据创建一个表。最后使用 INSERT INTO [DESTINATION_TABLE] select * from [TEMPORARY_SOURCE_TABLE]。将数据放入目标表后,删除临时表。

当我遇到同样的情况时,我找到了这个解决方案。希望这对你也有帮助。

于 2015-12-03T13:05:55.073 回答
3
USE [mydb1]

SELECT *
INTO mytable1
FROM OPENDATASOURCE (
        'SQLNCLI'
        ,'Data Source=XXX.XX.XX.XXX;Initial Catalog=mydb2;User ID=XXX;Password=XXXX'
        ).[mydb2].dbo.mytable2
    /*  steps - 
            1-  [mydb1] means our opend connection database 
            2-  mytable1 means create copy table in mydb1 database where we want insert record
            3-  XXX.XX.XX.XXX - another server name.
            4-  mydb2 another server database.
            5-  write User id and Password of another server credential
            6-  mytable2 is another server table where u fetch record from it. */
于 2018-04-04T05:53:09.970 回答
0

您可以使用CREATE SYNONYM来远程对象。

于 2013-01-04T08:37:28.463 回答