【问题标题】:How to get the actual script of a stored procedure in another server?如何在另一台服务器中获取存储过程的实际脚本?
【发布时间】:2020-03-03 20:38:11
【问题描述】:

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

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 服务器 A ([myMainServer].[myMainDatabase]) 中运行查询来获取 SQL 服务器 B ([myOtherServer].[myOtherDatabase]) 中的存储过程的脚本?

【问题讨论】:

标签: sql sql-server sql-server-2017


【解决方案1】:

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

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'

【讨论】:

    猜你喜欢
    • 2011-07-31
    • 2012-10-01
    • 2017-03-29
    • 1970-01-01
    • 2016-10-06
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多