在列中搜索列类型文本 SQL Server 的替换文本

发布于 2024-10-10 01:44:55 字数 1705 浏览 4 评论 0原文

我需要的是在表的特定列(数据类型: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九公里浅绿 2024-10-17 01:44:55

如果您无法永久更改列类型,则可以动态转换它们:

ALTER PROC [dbo].[SearchAndReplace] 
(@FindString    VARCHAR(100),
 @ReplaceString VARCHAR(100) )
AS
BEGIN
   UPDATE dbo.HTML_CONTENTS
   SET CONTENT_TEXT = cast (REPLACE(cast (CONTEXT_TEXT as varchar(max)), @FindString, @ReplaceString) as TEXT)
END

If you can't change your column types permanently, you can cast them on the fly:

ALTER PROC [dbo].[SearchAndReplace] 
(@FindString    VARCHAR(100),
 @ReplaceString VARCHAR(100) )
AS
BEGIN
   UPDATE dbo.HTML_CONTENTS
   SET CONTENT_TEXT = cast (REPLACE(cast (CONTEXT_TEXT as varchar(max)), @FindString, @ReplaceString) as TEXT)
END
反话 2024-10-17 01:44:55

数据类型 TEXT已弃用 并且不应该再使用 - 正是因为它很笨重并且不支持所有常用的字符串操作方法。

来自关于 text、ntext、image 的 MSDN 文档:

ntext、text 和 image 数据类型将
将在未来版本中删除
微软SQL服务器。 避免使用这些
新开发工作中的数据类型,
并计划修改应用程序
目前正在使用它们。 使用 nvarchar(max),
varchar(max) 和 varbinary(max)
相反


我的建议:将该列转换为 VARCHAR(MAX),之后应该就没问题了!

ALTER TABLE dbo.HTML_CONTENTS 
   ALTER COLUMN CONTEXT_TEXT VARCHAR(MAX)

应该可以做到这一点。

当您的列是 VARCHAR(MAX) 时,您的存储过程将变得完全简单:另外

ALTER PROC [dbo].[SearchAndReplace] 
(@FindString    VARCHAR(100),
 @ReplaceString VARCHAR(100) )
AS
BEGIN
   UPDATE dbo.HTML_CONTENTS
   SET CONTENT_TEXT = REPLACE(CONTEXT_TEXT, @FindString, @ReplaceString)
END

两个观察结果:

  • 在其中包含一个 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:

ntext, text, and image data types will
be removed in a future version of
MicrosoftSQL Server. Avoid using these
data types
in new development work,
and plan to modify applications that
currently use them. Use nvarchar(max),
varchar(max), and varbinary(max)
instead
.

My recommendation: convert that column to VARCHAR(MAX) and you should be fine after that!

ALTER TABLE dbo.HTML_CONTENTS 
   ALTER COLUMN CONTEXT_TEXT VARCHAR(MAX)

That should do it.

When your column is VARCHAR(MAX), then your stored procedures becomes totally simple:

ALTER PROC [dbo].[SearchAndReplace] 
(@FindString    VARCHAR(100),
 @ReplaceString VARCHAR(100) )
AS
BEGIN
   UPDATE dbo.HTML_CONTENTS
   SET CONTENT_TEXT = REPLACE(CONTEXT_TEXT, @FindString, @ReplaceString)
END

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 type NVARCHAR - try to stick to one set - either TEXT/VARCHAR(MAX) and regular VARCHAR(100) parameters, or then use all Unicode strings: NTEXT/NVARCHAR(MAX) and NVARCHAR(100). Constantly mixing those non-Unicode and Unicode strings is a mess and causes lots of conversions and unnecessary overhead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文