我需要的是在表的特定列(数据类型:)中搜索字符串text
并将其替换为另一个文本。
例如
Id | Text
-----------------------------
1 this is test
2 that is testosterone
如果我选择用测验代替测试,结果应该是
this is quiz
that is quizosterone
到目前为止我尝试了什么?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[SearchAndReplace]
(
@FindString NVARCHAR(100)
,@ReplaceString NVARCHAR(100)
)
AS
BEGIN
SET NOCOUNT ON
SELECT CONTENT_ID as id, CONTENT_TEXT, textptr(CONTENT_TEXT) as ptr, datalength(CONTENT_TEXT) as lng
INTO #newtable6 FROM HTML_CONTENTS
DECLARE @COUNTER INT = 0
DECLARE @TextPointer VARBINARY(16)
DECLARE @DeleteLength INT
DECLARE @OffSet INT
SELECT @TextPointer = TEXTPTR(CONTENT_TEXT)
FROM #newtable6
SET @DeleteLength = LEN(@FindString)
SET @OffSet = 0
SET @FindString = '%' + @FindString + '%'
WHILE (SELECT COUNT(*)
FROM #newtable6
WHERE PATINDEX(@FindString, CONTENT_TEXT) <> 0) > 0
BEGIN
SELECT @OffSet = PATINDEX(@FindString, CONTENT_TEXT) - 1
FROM #newtable6
WHERE PATINDEX(@FindString, CONTENT_TEXT) <> 0
UPDATETEXT #newtable6.CONTENT_TEXT
@TextPointer
@OffSet
@DeleteLength
@ReplaceString
SET @COUNTER = @COUNTER + 1
END
select @COUNTER,* from #newtable6
drop table #newtable6
SET NOCOUNT OFF
我得到错误:
消息 7116,级别 16,状态 4,过程 SearchAndReplace,第 31 行
偏移量 1900 不在可用 LOB 数据的范围内。
该语句已终止。
谢谢