简体   繁体   English

如何在SQL Server中使用不同数据库的表

[英]How to use table of different database in SQL Server

I have to transport some tables(16 tables) to another database and there are a number of stored procedures(200 tables) which use these tables. 我必须将一些表(16个表)传输到另一个数据库,并且有许多存储过程(200个表)使用这些表。

Transporting stored procedures to another database is not preferred. 不希望将存储过程传输到另一个数据库。

For my case: 对于我的情况:

dbA contains sp_xyz stored procedure, tableB and tableC tables. dbA包含sp_xyz存储过程, tableBtableC表。

and content of sp_xyz can be : sp_xyz内容可以是:

 SELECT A.column1, B.column2 
 FROM
 tableB A 
 JOIN tableC B ON A.fk_b_id = B.id

we want to transport tableC to dbB. 我们想将tableC传输到dbB。 So how should I change this sp with minimum change. 那么我应该如何以最小的改变来改变这个sp。

If you want to use a table in another database then you can do like this in sql server when the database is on same server: 如果要在另一个数据库中使用表,那么当数据库位于同一服务器上时,您可以在sql server中执行此操作:

Select * from [DBName].[Schema].[Table]

If the database is in another server, specify the linked server name too: 如果数据库位于另一台服务器中,请同时指定链接的服务器名称:

Select * from [DBServer].[DBName].[Schema].[Table]

Schema name - replace by your schema which is "dbo" by default in sql server. 架构名称 - 在sql server中默认替换为“dbo”的架构。

I tried a query for this and found that you can use 我尝试了一个查询,发现你可以使用

SELECT * FROM DB_Name.Schema.Table_Name

eg 例如

SELECT * FROM abcDB.dbo.address

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM