本質上,我試圖循環遍歷@sprocs
變量的內容,實現替換函數並打印相應的更改。當我執行代碼時,@spname
變量被打印出來,而不是預期的結果。問題使用SQL替換函數變量
declare @sprocs cursor
declare @spname nvarchar(max)
set @sprocs = cursor for
select ROUTINE_DEFINITION
from INFORMATION_SCHEMA.ROUTINES
where ROUTINE_TYPE = 'Procedure' AND
ROUTINE_DEFINITION like '%someString%'
open @sprocs
fetch next from @sprocs into @spname
while @@FETCH_STATUS = 0
Begin
set @spname = replace(@spname, '%someString%', 'Hello')
print @spname
fetch next from @sprocs into @spname
end
預期的結果是這樣的:
Before
Lorem存有悲坐阿梅德,someString adipiscing ELIT,sed的做eiusmod tempor incididunt UT labore等dolore麥格納aliqua。請將您的評論發送給我們,我們會盡快給您答覆。 Duis aute irure dolor in renhenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 someString s occaecat cupidatat non some String,sunt in culpa qui officia deserunt mollit anim id est laborum。
After
Lorem存有悲坐阿梅德,您好adipiscing ELIT,sed的做eiusmod tempor incididunt UT labore等dolore麥格納aliqua。你是否願意嘗試運動ullamco你好嗎? Duis aute irure dolor in renhenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。您好,請注意,您好,請告訴我們您的問題。
我認爲 - 最初 - 這可能是@spname
變量類型的問題,但由於它被宣佈爲nvarchar(max)
,我不能看到這是問題。
什麼能阻止預期的打印輸出?
您可能想使用replace(@spname,'someString','Hello')'而不是'replace(@spname,'%someString%','Hello')'..要替換的字符串應該被宣佈**字面上**通配符不工作在這裏。 –
@vpk,工作。我顯然不明白通配符是如何工作的。我知道你說過,這是因爲我在這種情況下尋找的字符串是字面聲明的,但爲什麼通配符在這種情況下不起作用?通配符是不是表示可以在字符串的任何部分找到它? – cwanjt
@vpk,如果你添加你的評論作爲答案,我會很樂意接受它。 – cwanjt