gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:37:04 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com