简体   繁体   English

SQL选择文本的一部分

[英]SQL select part of text

I'm looking to store the text of a stored procedure into a variable. 我希望将存储过程的文本存储到变量中。 I have the data appearing in a column, but I only want the data within the ALTER. 我的数据显示在列中,但我只希望ALTER中的数据。 I'm not that experienced with SQL, any help would be appreciated. 我对SQL的经验不是很丰富,可以提供任何帮助。 Thanks. 谢谢。

SELECT ?????????????????????
FROM    sysobjects 
JOIN    sys.sql_modules 
        ON      sysobjects.id = sys.sql_modules.object_id
WHERE name = stored_procedure_name

--The column's name is 'definition'

You can stored the text into a variable as so: 您可以这样将文本存储到变量中:

DECLARE @proctext NVARCHAR(MAX)

SELECT @proctext = definition
FROM    sysobjects 
JOIN    sys.sql_modules 
        ON      sysobjects.id = sys.sql_modules.object_id
WHERE name = stored_procedure_name

I then recommend playing with the SUBSTRING , LEN , LEFT , and RIGHT functions of SQL Server to get the rest. 然后,我建议使用SQL Server的SUBSTRINGLENLEFTRIGHT函数来获取其余功能。

Also, you will most likely want to find the location of CREATE , BEGIN , and or END in the definition when using the functions. 同样,使用这些函数时,您很可能希望在定义中找到CREATEBEGIN和或END的位置。

Something like this might work. 这样的事情可能会起作用。 Haven't tested. 还没测试。 Will need to play with the numbers used in SUBSTRING function to get the right result. 需要使用SUBSTRING函数中使用的数字才能获得正确的结果。

SELECT SUBSTRING([definition], CHARINDEX([definition], 'alter', 0) + 5 , LEN([definition]))
FROM    sysobjects 
JOIN    sys.sql_modules 
        ON      sysobjects.id = sys.sql_modules.object_id

Try this: 尝试这个:

select ALTER_DEF=STUFF([definition],PATINDEX('%CREATE%',[definition]),6,'ALTER')
from sys.sql_modules s
inner join sys.procedures p on s.object_id=p.object_id

I thinks you are using Microsoft SQL Server (sysobjects). 我认为您正在使用Microsoft SQL Server(sysobjects)。 To do this you need to ask for data "syscomments": 为此,您需要询问数据“ syscomments”:

select ctext from syscomments where id = object_id(N'procedurename')

Ctext contain procedure content! Ctext包含程序内容!

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

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