Jay*_*ayJ 2 sql sql-server stored-procedures
如何在存储过程中查找和替换特定文本?
我有查找和替换查询但我不知道如何将其发布回存储过程或使用查询结果更新存储过程.
这是我用于查找和替换的查询:
SELECT
REPLACE (object_definition(object_id('storedprocedure_1')), 'findstring', 'replacestring')
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这就是我能够提出的,它目前正在替换文本并重新创建存储过程.
| 归档时间: |
|
| 查看次数: |
4829 次 |
| 最近记录: |