在列中搜索列类型文本 SQL Server 的替换文本
我需要的是在表的特定列(数据类型: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 数据的范围内。
该声明已终止。
谢谢
What I need is to search for a string in a specific column (datatype: text
) of a table and replace it with another text.
For example
Id | Text
-----------------------------
1 this is test
2 that is testosterone
If I chose to replace test with quiz, results should be
this is quiz
that is quizosterone
What I've tried so far?
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
I get the error:
Msg 7116, Level 16, State 4, Procedure SearchAndReplace, Line 31
Offset 1900 is not in the range of available LOB data.
The statement has been terminated.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您无法永久更改列类型,则可以动态转换它们:
If you can't change your column types permanently, you can cast them on the fly:
数据类型
TEXT
已已弃用 并且不应该再使用 - 正是因为它很笨重并且不支持所有常用的字符串操作方法。来自关于 text、ntext、image 的 MSDN 文档:
我的建议:将该列转换为 VARCHAR(MAX),之后应该就没问题了!
应该可以做到这一点。
当您的列是
VARCHAR(MAX)
时,您的存储过程将变得完全简单:另外两个观察结果:
在其中包含一个
WHERE
子句会很有帮助你的存储过程,为了不更新整个表(除非那是你真正需要做的)您在表中使用
TEXT
,但存储过程参数的类型为NVARCHAR
- 尝试坚持使用一组 -TEXT/VARCHAR(MAX)
和常规VARCHAR(100)
参数,或者使用所有 Unicode 字符串:NTEXT/NVARCHAR(MAX)
和NVARCHAR(100)
。不断混合这些非 Unicode 和 Unicode 字符串会造成混乱,并导致大量转换和不必要的开销The datatype
TEXT
is deprecated and should not be used anymore - exactly because it's clunky and doesn't support all the usual string manipulation methods.From the MSDN docs on text, ntext, image:
My recommendation: convert that column to VARCHAR(MAX) and you should be fine after that!
That should do it.
When your column is
VARCHAR(MAX)
, then your stored procedures becomes totally simple:Two observations on the side:
it would be helpful to have a
WHERE
clause in your stored proc, in order not to update the whole table (unless that's what you really need to do)you're using
TEXT
in your table, yet your stored procedure parameters are of typeNVARCHAR
- try to stick to one set - eitherTEXT/VARCHAR(MAX)
and regularVARCHAR(100)
parameters, or then use all Unicode strings:NTEXT/NVARCHAR(MAX)
andNVARCHAR(100)
. Constantly mixing those non-Unicode and Unicode strings is a mess and causes lots of conversions and unnecessary overhead