SQL 我可以从另一个数据库服务器在我的数据库服务器中创建视图吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15573796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:25:01  来源:igfitidea点击:

Can I create view in my database server from another database server

sqlsql-server-2008viewconnection

提问by Huzaifa

Is it possible to create view in my database server of another servers database table?

是否可以在我的数据库服务器中创建另一个服务器数据库表的视图?

Let's say you have a database called Testingon server1and you have another database Testing2on server2. Is it possible to create view of Testing2's table Table2in server1's database Testing?

比方说,你有一个叫做数据库Testingserver1,你有其他数据库Testing2server2。是否可以在数据库中创建Testing2表的视图?Table2server1Testing

Also, I am using SQL Server 2008.

另外,我使用的是 SQL Server 2008。

Please let me know if you have any questions.

请让我知道,如果你有任何问题。

Thanks,

谢谢,

回答by Gordon Linoff

Yes, you can. First, you need to link to the other server, using something like sp_addlinkedserver.

是的你可以。首先,您需要使用类似sp_addlinkedserver.

Then you can access the data using 4-part naming. Here is an example:

然后您可以使用 4 部分命名访问数据。下面是一个例子:

create view v_server1_master_tables as
    select *
    from server1.master.information_schema.tables;

回答by Adam Luniewski

It is possible through linked servers. However, I wouldn't encourage you to create views based on tables from another server, as it's likely that entire table will be selected from linked server every time you use this view - optimizer may not know about this table structure to issue any filters. I've seen it at work, where nobody knew where select * fromqueries on large table come from that were slowing down the database, and it appeared that it was being used somwhere in another server, in a simple query. At least you should check if your solution won't cause the above problem. Maybe someone else could elaborate on how optimizer behave when dealing with linked servers?

可以通过链接服务器。但是,我不鼓励您基于来自另一台服务器的表创建视图,因为很可能每次使用此视图时都会从链接服务器中选择整个表 - 优化器可能不知道此表结构以发出任何过滤器。我在工作中看到过它,没有人知道select * from大表上的查询来自哪里,这会减慢数据库的速度,而且似乎在另一个服务器的某个地方,在一个简单的查询中使用了它。至少您应该检查您的解决方案是否不会导致上述问题。也许其他人可以详细说明优化器在处理链接服务器时的行为?