如何获取另一台服务器中存储过程的实际脚本?

How to get the actual script of a stored procedure in another server?

我使用 SQL Server 2017。我已将 server\database 链接到另一个我的主数据库。所以,如果我执行下面的查询,它会工作并给我带来正确的数据:

use [myMainServer].[myMainDatabase] GO

...
Select * from [myOtherServer].[myOtherDatabase].dbo.[myTable]

我想做的是从链接数据库中获取存储过程的实际脚本。假设我有存储过程:sp_GetNumbers,那么如果我在链接服务器本身中执行下面的代码,我就可以接收到它的内容:

SELECT OBJECT_DEFINITION(OBJECT_ID('sp_GetNumbers'))

但是我无法从我的主数据库中做到这一点。我在下面尝试过但没有用。

SELECT OBJECT_DEFINITION(OBJECT_ID('[myOtherServer].[myOtherDatabase].sp_GetNumbers'))

我的问题是:如何通过运行查询获取SQL服务器B([myOtherServer].[myOtherDatabase])中存储过程的脚本在 SQL 服务器 A ([myMainServer].[myMainDatabase])?

对象函数是特定于上下文的,因此不起作用,但您可以使用系统视图。像这样:

SELECT [definition] 
from [myOtherServer].[myOtherDatabase].sys.sql_modules m 
inner join [myOtherServer].[myOtherDatabase].sys.objects o
on m.[object_id] = o.[object_id]
where o.[name] = 'sp_GetNumbers'