AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 151550
Accepted
Philᵀᴹ
Philᵀᴹ
Asked: 2016-10-07 00:11:58 +0800 CST2016-10-07 00:11:58 +0800 CST 2016-10-07 00:11:58 +0800 CST

SQL Server - NTEXT 列和字符串操作

  • 772

我有一个表,其中有一NTEXT列名为comments. 我有第二个字符串,我们称它为anothercomment(a varchar),它需要放在commentsword 之后的给定字符串中UPDATEHERE。

转换为nvarchar(max)截断comments字符串,所以我不能使用CHARINDEX()(之类的Msg 8152, Level 16, State 10, Line 2 String or binary data would be truncated.)。我曾经datalength()检查过有几千列 > 8000 个字符。

我想要实现的一个示例(尽管字符串更长):

注释 -This is a test UPDATEHERE This is the end of the test

另一条评论——. This is inserted.

结果字符串 - This is a test UPDATEHERE. This is inserted. This is the end of the test

我意识到这对于普通的varchar()/来说是微不足道的nvarchar(),但却ntext是一个彻头彻尾的噩梦。我意识到这是一种已弃用的数据类型,但我没有编写有问题的应用程序。

sql-server sql-server-2008-r2
  • 2 2 个回答
  • 8708 Views

2 个回答

  • Voted
  1. Paul White
    2016-10-07T13:08:40+08:002016-10-07T13:08:40+08:00

    从代码的角度来看,转换nvarchar(max)和返回ntext确实让生活变得更简单,但这确实意味着转换和重写整个(可能非常大的)值,这意味着所有的 CPU 和日志记录开销。

    另一种方法是使用UPDATETEXT. 这已被弃用,就像 一样ntext,但它可以显着减少日志记录开销。不利的一面是,这意味着使用文本指针,并且一次只能操作一行。

    以下示例代码使用游标来解决该限制,并使用PATINDEX而不是CHARINDEX因为前者是直接使用的少数函数ntext之一:

    样本数据

    CREATE TABLE dbo.PhilsTable
    (
        comment ntext NULL,
        anothercomment nvarchar(50) NULL
    );
    
    INSERT dbo.PhilsTable
        (comment, anothercomment)
    VALUES 
    (
        CONVERT(ntext, 
            N'This is a test UPDATEHERE This is the end of the test ' + 
                REPLICATE (CONVERT(nvarchar(max), N'x'), 1000000)), 
        CONVERT(nvarchar(50), N'. This is inserted.')
    ),
    (
        CONVERT(ntext, 
            N'This is a test UPDATEHERE This is the end of the test ' + 
                REPLICATE (CONVERT(nvarchar(max), N'x'), 1000000)), 
        CONVERT(nvarchar(50), N'. This is inserted.')
    ),
    (
        CONVERT(ntext, 
            N'This is a test UPDATEHERE This is the end of the test ' + 
                REPLICATE (CONVERT(nvarchar(max), N'x'), 1000000)), 
        CONVERT(nvarchar(50), N'. This is inserted.')
    );
    

    光标声明

    DECLARE c 
        CURSOR GLOBAL 
        FORWARD_ONLY 
        DYNAMIC 
        SCROLL_LOCKS 
        TYPE_WARNING
    FOR
    SELECT
        TxtPtr = TEXTPTR(PT.comment),
        Src = PT.anothercomment,
        Offset = PATINDEX(N'%UPDATEHERE%', PT.comment) + LEN(N'UPDATEHERE') - 1
    FROM dbo.PhilsTable AS PT
    WHERE
        PT.comment LIKE N'%UPDATEHERE%'; -- LIKE works with ntext
    
    OPEN c;
    

    处理循环

    DECLARE 
        @Ptr binary(16),
        @Src nvarchar(50),
        @Offset integer;
    
    SET STATISTICS XML OFF; -- No cursor fetch plans
    
    BEGIN TRANSACTION;
    
        WHILE 1 = 1
        BEGIN
            FETCH c INTO @Ptr, @Src, @Offset;
    
            IF @@FETCH_STATUS = -2 CONTINUE; -- row missing
            IF @@FETCH_STATUS = -1 BREAK; -- no more rows
    
            IF 1 = TEXTVALID('dbo.PhilsTable.comment', @Ptr)
            BEGIN
                -- Modify ntext value
                UPDATETEXT dbo.PhilsTable.comment @Ptr @Offset 0 @Src;
            END;
        END;
    
    COMMIT TRANSACTION;
    
    CLOSE c; DEALLOCATE c;
    
    • 10
  2. Best Answer
    Tom V
    2016-10-07T00:47:31+08:002016-10-07T00:47:31+08:00

    nvarchar(max)除非你做错了什么,否则转换为应该工作CHARINDEX()

    试试这个代码片段,它应该输出你想要的。

    -- Create the table
    CREATE TABLE [dbo].[PhilsTable](
        [comment] [ntext] NULL,
        [anothercomment] [nvarchar](50) NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY];
    
    GO
    
    -- insert very long string
    INSERT INTO [dbo].[PhilsTable] (comment, anothercomment) VALUES (N'This is a test UPDATEHERE This is the end of the test' + REPLICATE (CAST(N'x' AS nvarchar(max)), 1000000), 'this goes in here');
    
    -- verify data
    SELECT DATALENGTH(comment), *  FROM [dbo].[PhilsTable];
    
    -- perform replace
    SELECT CAST(REPLACE(CAST(comment AS NVARCHAR(MAX)),'UPDATEHERE','UPDATEHERE' + anothercomment) AS NTEXT) FROM [dbo].[PhilsTable];
    
    DROP TABLE [dbo].[PhilsTable];
    

    感谢Andriy M提供的帮助REPLICATE。

    • 8

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve