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

时间:2022-01-02 01:52:33

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,还是将所有表合并到一个数据库中。检查以前的问题/答案,我了解到这取决于会员数据是否将在多个应用程序之间共享。

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?

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

Thanks for helping.

谢谢你的帮助。

1 个解决方案

#1


13  

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

#1


13  

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