如何查询位于不同数据库中的表?

2021-09-10 00:00:00 tsql sql-server

我最初的问题是关于是将 ASPNETDB.MDF 与应用程序数据库分开还是将所有表合并到一个数据库中.查看之前的问题/答案,我了解到这取决于会员数据是否会在多个应用程序之间共享.

My original question was about whether to keep separate ASPNETDB.MDF from the application database or merge all the tables in one database. Checking the previous questions/answers, I learned that it depends on whether the membership data would be shared across several applications.

现在,我的问题是这个.如果我决定将 ASPNETDB.MDF 与应用程序数据库分开,如何查询位于 2 个不同数据库中的 2 个表?

Now, my question is this. In case I decide to keep ASPNETDB.MDF separate from the application DB, how can I query 2 tables located in 2 different databases?

感谢您的帮助.

推荐答案

如果您在同一数据库服务器上有两个数据库/模式,您可以使用以下语法跨数据库查询:

If you have two databases/schemas on the same database server, you can query across databases with the following syntax:

select *
from database1.dbo.table1 t1 join database2.dbo.table2 t2 on
  t1.field1 = t2.field2

如果它们在物理上独立的服务器上,您仍然可以进行跨库查询,但您需要先链接服务器:

If they are on physically separate servers, you can still do a cross-database query, but you need to link the servers first:

http://msdn.microsoft.com/en-us/library/aa213778(v=sql.80).aspx

相关文章