Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找和替换存储过程中的文本的SQL查询_Sql_Sql Server_Stored Procedures - Fatal编程技术网

查找和替换存储过程中的文本的SQL查询

查找和替换存储过程中的文本的SQL查询,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,如何查找和替换存储过程中的特定文本 我有find和replace查询,但我不确定如何将其发回存储过程或使用查询结果更新存储过程 以下是我用于查找和替换的查询: SELECT REPLACE (object_definition(object_id('storedprocedure_1')), 'findstring', 'replacestring') 您可以获得存储过程的定义,如下所示: use <your database> go select definition fr

如何查找和替换存储过程中的特定文本

我有find和replace查询,但我不确定如何将其发回存储过程或使用查询结果更新存储过程

以下是我用于查找和替换的查询:

SELECT
   REPLACE (object_definition(object_id('storedprocedure_1')), 'findstring', 'replacestring')

您可以获得存储过程的定义,如下所示:

use <your database>
go
select
definition
from
sys.sql_modules
where
object_id = OBJECT_ID('<your schema.procedure_name>')
使用
去
选择
定义
从…起
sys.sql\u模块
哪里
对象id=对象id(“”)
这将返回指定过程的实际定义。我想,您可以构建一个存储过程列表,然后一次遍历一个存储过程。我不太确定是否建议尝试在sys.sql_模块中更改它们,但至少您可以找到包含您要查找的文本的模块

Declare @spnames CURSOR
Declare @spname nvarchar(max)
Declare @moddef nvarchar(max)
Set @spnames = CURSOR FOR
select distinct object_name(c.id) 
from syscomments c, sysobjects o 
where c.text like '%findtext%'
and c.id = o.id
and o.type = 'P'    
OPEN @spnames
FETCH NEXT
FROM @spnames into @spname
WHILE @@FETCH_STATUS = 0
BEGIN   
    Set @moddef =
    (SELECT
    Replace ((REPLACE(definition,'findtext','replacetext')),'ALTER','create')
    FROM sys.sql_modules a
    JOIN 
    (   select type, name,object_id
    from sys.objects b
    where type in (
    'p' -- procedures
    )
    and is_ms_shipped = 0
    )b
    ON a.object_id=b.object_id where b.name = @spname)              
    exec('drop procedure dbo.' + @spname)
    execute sp_executesql @moddef
    FETCH NEXT FROM @spnames into @spname
END

这就是我能够想到的,它目前正在进行文本替换并重新创建存储过程

我需要更新原始定义,这不是一次性的任务,每次数据库恢复时都必须完成。这就是我希望通过sql更新所有过程的原因。我可以使用.net存储过程对象(TextBody)实现同样的功能,但由于我要更新大约200多个SP,所以大约需要25-30分钟。因此,我想通过SQL路径看看它是否能在1小时内完成一天的任务。我们可以使用这段代码来更改其他类型的对象。我今天用它来更改触发器,在更改了大量触发器之后,将数据写入另一个模式。简单:通过'TR'更改o.type='P',通过drop触发器更改drop过程。谢谢@Jay J