将存储过程的文本获取到 SQL Server 中的变量中 [英] get the text of a stored procedure into a variable in SQL Server

查看:29
本文介绍了将存储过程的文本获取到 SQL Server 中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历多个存储过程并从每个过程中提取一个字符串以在另一个过程中使用(基本上是由 4 部分组成的远程服务器字符串)

I want to loop through several stored procs and extract one string form each of them to use in another procedure (basically the 4-part remote server string)

所以我可以从 SysObjects(Type = P)获取存储的 procs 列表到一个表中,然后我可以循环或游标通过该表变量调用 sp_helptext每一个.

So I can get the stored procs list from SysObjects (with Type = P) into a table, then I can loop or cursor through that table variable calling sp_helptext on each one.

但是如何将 sp_helptext 的文本结果放入一个变量中,以便我可以对BOCTEST"等词进行 CharIndex?

But how can I get the text result of sp_helptext into a variable so I can do a CharIndex on the word 'BOCTEST' etc?

是否有像 sys.procedures 这样的表来存储文本.

Is there a table like sys.procedures that stores the text.

推荐答案

可移植的方式是使用 ANSI/ISO 视图 INFORMATION_SCHEMA.ROUTINES,但你只能得到前 4000 个字符存储过程定义:

The portable way is to use the ANSI/ISO view INFORMATION_SCHEMA.ROUTINES, but you'll only get the first 4000 characters of the stored procedure definition:

declare @source_code varchar(max)

select @source_code = t.ROUTINE_DEFINITION
from information_schema.routines t
where specific_schema = 'owner-schema'             -- e.g., dbo
  and specific_name   = 'my_stored_procedure_name' -- your stored procedure name here

或者你可以同样使用系统视图sys.sql_modules:

Or you can use the system view sys.sql_modules in the same vein:

declare @source_code varchar(max)

select @source_code = definition
from sys.sql_modules
where object_id = object_id('dbo.my_stored_procedure_name')

或者,最简单的方法:

declare @source_code varchar(max)
set @source_code = object_definition( 'dbo.my_stored_procedure_name' )

这篇关于将存储过程的文本获取到 SQL Server 中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆